在上一次朋友问我如何快速构造一亿条记录的表后,我理出了实行的办法,见:http://blog.csdn.net/mchdba/article/details/52938114,但是因为录入一亿表需要2个小时,所以还是感觉速度慢了些,那有没有啥办法能加快这一步骤呢?
1、建一张通用的用户表
建用户表没有啥变化,还是和上次一样。
USE test; CREATE TABLE `UC_USER` ( `ID` BIGINT (20), `USER_NAME` VARCHAR (400), `USER_PWD` VARCHAR (800), `BIRTHDAY` DATETIME , `NAME` VARCHAR (800), `USER_ICON` VARCHAR (2000), `SEX` CHAR (4), `NICKNAME` VARCHAR (800), `STAT` VARCHAR (40), `USER_MALL` BIGINT (20), `LAST_LOGIN_DATE` DATETIME , `LAST_LOGIN_IP` VARCHAR (400), `SRC_OPEN_USER_ID` BIGINT (20), `EMAIL` VARCHAR (800), `MOBILE` VARCHAR (200), `IS_DEL` CHAR (4), `IS_EMAIL_CONFIRMED` VARCHAR (4), `IS_PHONE_CONFIRMED` VARCHAR (4), `CREATER` BIGINT (20), `CREATE_DATE` DATETIME , `UPDATE_DATE` DATETIME , `PWD_INTENSITY` VARCHAR (4), `MOBILE_TGC` VARCHAR (256), `MAC` VARCHAR (256), `SOURCE` VARCHAR (4), `ACTIVATE` VARCHAR (4), `ACTIVATE_TYPE` VARCHAR (4), `IS_LIFE` VARCHAR (4) ) ENGINE=INNODB; |
2、优化录数据的存储过程
这里有变化了,设置了set autocommit=0;了,这样不用每次insert就commit一次,实现了30W后insert然后进行commit,这样就是批量提交,提高了数据写效率。
而且在执行commit的时候,为了验证是否批量提交,就做了有小验证,而且验证的信息写入到临时日志表里面uc_log表去,uc_log表结构如下:
CREATE TABLE `uc_log` ( |
存储过程内容:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `pro_test_data_1`$$
CREATE DEFINER=`dev_user`@`%` PROCEDURE `pro_test_data_1`( pos_begin INT,pos_end INT) BEGIN DECLARE i INT; SET i=pos_begin; SET AUTOCOMMIT=0; WHILE i>=pos_begin && i<= pos_end DO
INSERT INTO test.`UC_USER_1` (`ID`, `USER_NAME`, `USER_PWD`, `BIRTHDAY`, `NAME`, `USER_ICON`, `SEX`, `NICKNAME`, `STAT`, `USER_MALL`, `LAST_LOGIN_DATE`, `LAST_LOGIN_IP`, `SRC_OPEN_USER_ID`, `EMAIL`, `MOBILE`, `IS_DEL`, `IS_EMAIL_CONFIRMED`, `IS_PHONE_CONFIRMED`, `CREATER`, `CREATE_DATE`, `UPDATE_DATE`, `PWD_INTENSITY`, `MOBILE_TGC`, `MAC`, `SOURCE`, `ACTIVATE`, `ACTIVATE_TYPE`, `IS_LIFE`) VALUES(i,'admin','1ba613b3676a4a06d6204b407856f374',NOW(),'超管','group1/M00/03/BC/wKi0d1QkFaWAHhEwAAAoJ58qOcg271.jpg','1','admin2014','01','1',NOW(),'192.168.121.103',NULL,'','10099990001','0','1','0',NULL,NULL,NULL,'1','E5F10CAA4EBB44C4B23726CBBD3AC413','1-3','0','2','2','1'); SET i=i + 1; # 接下来判断30W一批次就commit一回。 IF MOD(i,300000)<=0 THEN INSERT INTO test.uc_log(id,msg)VALUES(i,'begin to commmit a group insert sql data.'); COMMIT; END IF; END WHILE; END$$
DELIMITER ; |
Blog来源地址: http://blog.csdn.net/mchdba/article/details/52987852,博主mchdba(黄杉),谢绝转载
3、录入数据忽略binlog
开始启用存储过程录入一亿条数据进表,命令call test.pro_test_data(0,100000000);,这里时间会比较长一些,毕竟是一亿条记录。
# 录入数据的时候,不写入binlog,加快数据录入 mysql> set sql_log_bin=0; Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> call test.pro_test_data_1(0,100000000); Query OK, 1 row affected (1 hour 37 min34.57 sec) # 看到用时是1 hours 37 min 34.57 sec,1个半小时左右。
mysql> mysql> select count(1) from test.`UC_USER_1`; +-----------+ | count(1) | +-----------+ | 100000001 | +-----------+ 1 row in set (3 min 0.14 sec)
mysql> |