mysql存储中怎么使用while批量插入数据
作者:CalmWind
时间:2023-05-02
浏览:1
批量提交while语句写法:while'条件'do循环体语句;endwhile;完整写法dropprocedureifexiststest_insert;delimiter$$createproceduretest_insert(nint)begindeclarevintdefault0;setAUTOCOMMIT=0;whilevshowprocedurestatuslike'test_insert';mysql>showcreateproceduretest_inser
本文目录
批量提交
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)
作者最新文章
PS反选快捷键有哪些技巧
2026-10-29 08:45
xshell怎么快速删除文本 命令行清除技巧
2026-10-21 18:09
Luminar Neo人像修图指南:如何点亮眼神与适度放大双眼
2026-10-21 11:00
怎么隐藏Windows10任务栏上的输入法图标
2026-10-16 18:00
漫蛙网页版免费入口分享
2026-10-13 13:19
上一篇:
thinkphp5中的join怎么用
下一篇:
MySQL二级索引查询过程是怎么样的
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































