MySQL如何循环插入千万级数据?本篇文章小编给大家分享一下MySQL循环插入千万级数据代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1、创建测试表
CREATE TABLE `mysql_genarate` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uuid` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5999001 DEFAULT CHARSET=utf8;
2、创建一个循环插入的存储过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `test_two1`( ) BEGIN DECLARE i INT DEFAULT 0; WHILE i < 3000 DO INSERT INTO mysql_genarate ( uuid ) VALUES( UUID( ) ); SET i = i + 1; END WHILE; END
调用测试call test_two1(), 测试10000条数据耗时几分钟,如果是千万级数据,这个速度将无法忍受。
3、优化存储过程
使用批量插入的sql语句
CREATE DEFINER=`root`@`localhost` PROCEDURE `insertPro`( IN sum INT ) BEGIN DECLARE count INT DEFAULT 0; DECLARE i INT DEFAULT 0; SET @exesql = concat( "insert into mysql_genarate(uuid) values" ); SET @exedata = ""; SET count = 0; SET i = 0; WHILE count < sum DO SET @exedata = concat( @exedata, ",(UUID())" ); SET count = count + 1; SET i = i + 1; IF i % 1000 = 0 THEN SET @exedata = SUBSTRING( @exedata, 2 ); SET @exesql = concat( "insert into mysql_genarate(uuid) values ", @exedata ); PREPARE stmt FROM @exesql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @exedata = ""; END IF; END WHILE; IF length( @exedata ) > 0 THEN SET @exedata = SUBSTRING( @exedata, 2 ); SET @exesql = concat( "insert into mysql_genarate(uuid) values ", @exedata ); PREPARE stmt FROM @exesql; EXECUTE stmt; DEALLOCATE PREPARE stmt; END IF; END
调用 call insertPro(10000) ,耗时零点几秒,这个速度可以接受。