平常在做SQL优化时经常需要自己在测试环境中构建大量的数据,下面通过一个简单的存储过程来实现批量数据的插入过程。
1.创建测试表
mysql> show create table house\G
*************************** 1. row ***************************
Table: house
Create Table: CREATE TABLE `house` (
`unitid` int(11) DEFAULT NULL,
`housename` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`status` int(11) DEFAULT NULL,
KEY `unitid` (`unitid`),
KEY `index_name` (`housename`),
KEY `index_status_housename` (`status`,`housename`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.00 sec)
2.创建批量插入数据的存储过程
mysql> delimiter //
mysql> create procedure myproc()
-> begin
-> declare num int;
-> set num=1;
-> while num < 500000 do
-> insert into house values(num,concat('''1-',num,''''),mod(num,2));
-> set num=num+1;
-> end while;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
3.批量插入数据
mysql> call myproc
-> //
Query OK, 1 row affected (52.33 sec)
4.验证数据
mysql> select status,count(*) from house group by status;
-> //
+--------+----------+
| status | count(*) |
+--------+----------+
| 0 | 249999 |
| 1 | 250000 |
+--------+----------+
2 rows in set (0.16 sec)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/20801486/viewspace-2120771/
,如需转载,请注明出处,否则将追究法律责任。