曾经一个朋友问我如何快速的在线往一个大表里面添加一个字段或者修改一个字段的长度,mysql版本是5.6,所以就准备在测试环境准备一个一亿条记录的表,然后来实际测试下到底哪种方式比较快,先来开始准备一亿条记录的表。
我线上有上亿条记录的表,但是很多网上朋友都没有,那么我这里就实践了一条办法,来实现自己构造一亿条数据记录的表。实现思路就是先建一张通用的20个字段左右的用户表,然后写一个存储过程,不停的往这个表里面写数据,while循环写上一亿次,这样就形成了一张一亿条记录的表出来。
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、准备录入数据的存储过程
存储过程内容:
DELIMITER $$ USE `test`$$ DROP PROCEDURE IF EXISTS test.`pro_test_data`$$ CREATE PROCEDURE test.`pro_test_data`( pos_begin INT,pos_end INT) BEGIN DECLARE i INT; SET i=pos_begin; WHILE i>=pos_begin && i<= pos_end DO INSERT INTO test.`UC_USER` (`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; END WHILE; END$$ DELIMITER ; |
Blog来源地址:http://blog.csdn.net/mchdba/article/details/52938114,博主mchdba(黄杉),谢绝转载
在sql命令窗口界面执行过程如下:
mysql> DELIMITER $$ mysql> USE `test`$$ Database changed mysql> DROP PROCEDURE IF EXISTS test.`pro_test_data`$$ Query OK, 0 rows affected (0.00 sec) mysql> CREATE PROCEDURE test.`pro_test_data`( pos_begin INT,pos_end INT) -> BEGIN -> DECLARE i INT; -> SET i=pos_begin; -> WHILE i>=pos_begin && i<= pos_end DO -> Display all 1421 possibilities? (y or n) -> INSERT INTO test.`UC_USER` (`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‘); -> -> Display all 1421 possibilities? (y or n) -> SET i=i + 1; -> END WHILE; -> END$$ Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ; mysql> |
3、开始录入数据
开始启用存储过程录入一亿条数据进表,命令call test.pro_test_data(0,100000000);,这里时间会比较长一些,毕竟是一亿条记录。
mysql> call test.pro_test_data(0,100000000); Query OK, 1 row affected (2 hours 26 min 56.32 sec) # 看到用时是2 hours 26 min 56.32 sec,2个半小时。 mysql> select count(1) from test.`UC_USER`; +-----------+ | count(1) | +-----------+ | 100000001 | +-----------+ 1 row in set (3 min 0.14 sec) mysql> |
4、清除binlog日志
因为录入了一亿条记录,所以会产生大量的binlog日志,我们需要清除这些binlog记录,免得占据磁盘空间:
mysql> show master status; +------------------+----------+--------------+---------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+---------------------------------------+-------------------+ | mysql-bin.007143 | 4589266 | | information_schema,performance_schema | | +------------------+----------+--------------+---------------------------------------+-------------------+ 1 row in set (0.01 sec) mysql> purge master logs to "mysql-bin.0007143"; ERROR 1373 (HY000): Target log not found in binlog index mysql> purge master logs to "mysql-bin.007143"; Query OK, 0 rows affected (5.44 sec) mysql> |