mysql存储中怎么使用while批量插入数据
作者:CalmWind
时间:2023-04-28
来源:互联网
浏览:0
批量提交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)
作者最新文章
PDF转图片在线怎么用?资料整理的简单流程
2026-09-03 12:12
科大讯飞发布星火多模态大模型X2-VL,基于全国产算力训练
2026-08-25 16:21
雷军小米YU7装600斤车厘子慰问工程师被指违规 回应:封闭道路分装 交警称后排满载不合法
2026-08-25 15:23
Anthropic禁用Fable 5模型,亚马逊CEO贾西或是背后导火索
2026-08-25 14:58
长虹T06(双4G)忘了手机密码怎么办?
2026-08-25 13:40
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















