饿虎岗资源网 Design By www.oxmxm.com

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循环插入的资料请关注其它相关文章!

饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com