当前位置:网站首页>MySQL在存储过程中使用while批量插入数据(批量提交和单个提交的性能差异)
MySQL在存储过程中使用while批量插入数据(批量提交和单个提交的性能差异)
2022-06-09 08:44:00 【刘大大__】
批量提交:
while 语句写法:
while '条件' do
循环体语句;
end while;
完整写法:
drop procedure if exists test_insert;
delimiter $$
create procedure test_insert(n int)
begin
declare v int default 0;
set AUTOCOMMIT = 0;
while v < n
do
insert into test(second_key, text, field_4,status, create_date)
values ((v*10),
concat('t',v),
substring(md5(rand()), 1, 10),
'good',
adddate('1970-01-01', rand(v) * 10000));
set v = v + 1;
end while;
set AUTOCOMMIT = 1;
end$$
delimiter ;
查看、删除存储过程:
mysql> show procedure status like 'test_insert';
mysql> show create procedure test_insert\G;
mysql> drop procedure if exists test_insert;
创建表
CREATE TABLE test (
id INT NOT NULL AUTO_INCREMENT,
second_key INT,
text VARCHAR(20),
field_4 VARCHAR(20),
status VARCHAR(10),
create_date date,
PRIMARY KEY (id),
KEY idx_second_key (second_key)
) Engine=InnoDB CHARSET=utf8;
插入100万条数据
mysql> call test_insert(1000000);
Query OK, 0 rows affected (31.86 sec)
单个提交:
完整写法:
drop procedure if exists test_insert;
delimiter $$
create procedure test_insert(n int)
begin
declare v int default 0;
while v < n
do
insert into test(second_key, text, field_4,status, create_date)
values ((v*10),
concat('t',v),
substring(md5(rand()), 1, 10),
'good',
adddate('1970-01-01', rand(v) * 10000));
set v = v + 1;
end while;
end$$
delimiter ;
插入1万条数据
mysql> call test_insert(10000);
Query OK, 1 row affected (1 min 8.52 sec)
打开另一个窗口查看
mysql> select count(*) from test.test;
+----------+
| count(*) |
+----------+
| 1428 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from test.test;
+----------+
| count(*) |
+----------+
| 1598 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from test.test;
+----------+
| count(*) |
+----------+
| 1721 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from test.test;
+----------+
| count(*) |
+----------+
| 1983 |
+----------+
1 row in set (0.00 sec)
结论:
批量提交100万条数据用了30秒,单个提交1万条数据用了1分钟,对比发现,批量提交的效率远大于单个提交的效率
边栏推荐
- SQL: 销售分析III
- Configuring the environment for RMAN backups_ Automatic backup of configuration control files and server parameter files
- Configuring the environment for RMAN backups
- Simple use of vim
- RMAN备份概念_关于备份保留策略(RETENTION POLICY)
- 49-oauth2 pkce configuration in authorization code
- Self made compiler learning 3: introduction to flex and bison
- Configure CONDA environment and pytoch installation
- 腾讯云宝塔搭建网站注意事项
- Use of cookies, sessions and tokens in web development of system development series
猜你喜欢
随机推荐
Nodejs使用Net模块创建TCP服务器和客户端
[program life] internet job division; Internet development process; Division of responsibilities
安科瑞Acrel-BUS智能照明控制系统在医院的应用
3D編程模式:依賴隔離模式
阿里云IOT
vim的简单使用
Common windbos APIs
Sql: median employee salary
Sql: capital gains and losses of stocks
Configure CONDA environment and pytoch installation
Oracle locally managed tablespaces
web知识点123
10 useful flutter widgets
Creation of JS class and use of constructor constructor
RMAN备份概念_关于RMAN控制文件和服务器参数文件的自动备份
SQL: 超过经理收入的员工
使用闪回数据库(FLASHBACK DATABASE)和还原点(RESTORE POINT)
框架初入门:更新表达式无效 ,必须至少更新一列
RMAN备份概念_关于RMAN备份的多个拷贝
About Matrix Decompositions









