您好,欢迎访问一九零五行业门户网

mysql存储中怎么使用while批量插入数据

批量提交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)
以上就是mysql存储中怎么使用while批量插入数据的详细内容。
其它类似信息

推荐信息