mysql> show create procedure pro_addusers\G
*************************** 1. row ***************************
Procedure: pro_addusers
sql_mode:
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_addusers`(userconut int)
begin
set
@i = 0;
repeat set
@i =
@i + 1;
insert into users values (
@var :=replace(uuid(),'-',''),left(
@var ,10),
@var ,'abcdefghijglmn');
until
@i = userconut end repeat;
end
1 row in set (0.00 sec)
mysql> show create procedure pro_addusers\G
*************************** 1. row ***************************
Procedure: pro_addusers
sql_mode:
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_addusers`(userconut int)
begin
set autocommit=0;
set
@i = 0;
repeat set
@i =
@i + 1;
insert into users values (
@var :=replace(uuid(),'-',''),left(
@var ,10),
@var ,'abcdefghijglmn');
if @i%1000=0 then commit;
end if;
until
@i = userconut end repeat;
end
1 row in set (0.00 sec)
上面兩個存儲過程,向同一個INNODB表插入一樣的數據量,時間差兩個數量級。
插入10萬條記錄,前者10分鐘,後者1秒鐘。
因此對INNODB表批量處理數據的時候,最好不要用自動提交。