本篇内容介绍了“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批量插入数据”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。