一些测试场景的执行往往需要先往DB中注入一些测试数据,一般可以通过sql脚本来实现
关于mysql存储过程的介绍可以参考http://tech.huweishen.com/weihu/1722.html,以下是自己写的一个简单例子
drop procedure if exists init_QB_Static_Info;
delimiter //
create procedure init_QB_Static_Info ()
begin
declare i int default 0;
delete from tQuestionBank_Static_Info;
while(i < 1000) do
insert into tQuestionBank_Static_Info values(i, mod(i, 10), CONCAT('Test', i), 'filepath:xxxxxx', i, 10.1, 'Test Question', now(), 0);
set i = i + 1;
end while;
end;
drop procedure if exists init_QB_Access_Info;
create procedure init_QB_Access_Info ()
begin
declare i int default 0;
delete from tQuestionBank_Access_Info;
while(i < 100000) do
-- if (mod(i , 2) = 0) then
insert into tQuestionBank_Access_Info values(1, mod(i, 1000), mod(i, 10), 1, now());
-- end if;
set i = i + 1;
end while;
end;
//
delimiter ;
执行上面存储过程,发现插入记录非常慢
mysql> source /data/Work/bubinglv-service/sql-script/test.sql
mysql> call init_QB_Static_Info;
Query OK, 1 row affected (37.27 sec)
mysql> call init_QB_Access_Info;
Query OK, 1 row affected (1 hour 6 min 7.13 sec)
对于大数据量的插入,比较高效的做法是采用通过文件加载数据的方式,需要mysql的root用户来执行
从数据库导出数据到文件
mysql> select * from tQuestionBank_Access_Info into outfile './qb_access_info.txt' fields terminated by '.' lines terminated by '\r\n';
Query OK, 100000 rows affected, 1 warning (0.09 sec)
导出的文件存放在/var/lib/mysql/qb_access_info.txt
从文件导入数据到数据库
mysql> delete from tQuestionBank_Access_Info;
Query OK, 100000 rows affected (0.99 sec)
mysql> load data infile './qb_access_info.txt' into table tQuestionBank_Access_Info fields terminated by '.' lines terminated by '\r\n';
Query OK, 100000 rows affected (1.19 sec)
Records: 100000 Deleted: 0 Skipped: 0 Warnings: 0