awk批量插入

awk批量插入,速度很快的,150万条记录,3分钟左右吧

执行:awk -f /tmp/push_info_insert.awk /tmp/221.txt


BEGIN{
    FS=","   
	taskId="20051"
	user="user"
	passwd="password"
	host="localhost"
    print "==============start=================="
    sqlInsert="INSERT INTO mobile.mobile_push_info (token,task_id,create_time) VALUES"
	sql=sqlInsert
    idx=0
	conMysql="mysql -h"host" -u"user" -p'"passwd "' mobile -e "
    "date +%Y-%m-%d\ %H:%M:%S" | getline time;print time;	
}

{
	idx=idx+1
	#每1000条记录拼接长一条sql
	if(idx==1000){		
		sqlValues="('"$0"', "taskId",'2017-02-22');"		
		sql=sql sqlValues		
		str=conMysql "\"" sql "\" "
		system(str)
		idx=0
		sql=sqlInsert
	}else{		
		sqlValues="('"$0"', "taskId",'2017-02-22'),"
		sql=sql sqlValues
	}
}

END{         
	#去掉最后的分号
	sql= substr(sql, 0, length(sql))	
    
	str=conMysql "\"" sql "\" "
	
	#执行最后不足1000条记录的sql
	if(idx>0 && idx<1000){
		system(str)
		print"=============end============idx====="idx	
	}	
	
    "date +%Y--%m-%d\ %H:%M:%S" | getline time2;print time2
    print"=============end================="	

}















 

发表评论

邮箱地址不会被公开。