普通网友 2015-03-16 08:09 采纳率: 66.7%
浏览 3223
已采纳

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

  • 写回答

4条回答

  • oyljerry 2015-03-16 08:11
    关注

    最好的办法是把文件组织成CSV格式,然后用LOAD DATA infile导入mysql,适合批量。性能也好

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛