在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 调用测试 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) ,耗时零点几秒,这个速度可以接受。 以上就是MySQL循环插入千万级数据的详细内容,更多关于MySQL循环插入的资料请关注极客世界其它相关文章! |
请发表评论