您好,欢迎来到意题教育。
搜索
您的当前位置:首页关于mysql使用存储过程插入大量数据的问题

关于mysql使用存储过程插入大量数据的问题

来源:意题教育


mysql大量数据插入存储过程

创建数据库
create database ceshi;
use ceshi;
创建表
create table ce(
id int not null auto_increment,
name varchar(50) default null,
pw char(50),
primary key(id));

存储过程
delimiter //
create procedure insert_ce(in item int)
begin
declare counter int;
declare i int;
set counter=item;
while counter >=1 do
insert into ce values(counter,concat('mysqls',counter),repeat('ab',5));
set counter =counter-1;
set i=i+1;
if i=1000 then
set i=0;
commit;
end if;
end while;
end
//
delimiter ;
十万条数据使用的时间是40分钟

另一钟方法:
create table t_2 (id serial,name char(5)) ;
delimiter $$
SET AUTOCOMMIT = 0$$

create procedure test02(in i int)
begin
declare v_cnt decimal (10) default 0 ;
dd:loop
insert into t_2 values
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa'),
(null,'aaaaa');
commit;
set v_cnt = v_cnt+10 ;
if v_cnt = i then leave dd;
end if;
end loop dd ;
end;$$

delimiter ;
十万条数据使用的时间是7分钟
设置缓存的话不到两种方法都是一份钟不到就好了,朋友说有可能是默认缓存过小的原因。可是,两种方法在同一个机子上测试,没有设置缓存,所用的时间就是差很多。
想问一下是什么原因造成的,还想知道插入多少条数据提交一次才是最优插入,第二种方法的注释?

Copyright © 2019- itiz.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务