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

发布时间:2022-08-17 16:50:04 作者:iii
来源:亿速云 阅读:149

本篇内容介绍了“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批量插入数据”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. ORACLE存储过程while循环的使用
  2. Django中mysql使用事务以及批量插入数据

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql while

上一篇:Broadcast广播机制在Pytorch Tensor Numpy中如何使用

下一篇:elementui时间日期选择器el-date-picker报错Prop being mutated:"placement"如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》