MySQL循环插入千万级数据
(编辑:jimmy 日期: 2024/12/26 浏览:3 次 )
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) ,耗时零点几秒,这个速度可以接受。
以上就是MySQL循环插入千万级数据的详细内容,更多关于MySQL循环插入的资料请关注其它相关文章!
下一篇:简单了解mysql存储字段类型查询效率