1. 首页 > ITPUX技术网 > 正文

MySQL如何快速创建大量随机数据进行性能测试

MySQL如何快速创建大量随机数据进行性能测试

思路:创建procedure,然后使用循环,不停insert数据。例如:
create procedure inst(n int)
begin
declare i int default 0;
set autocommit = 0;
repeat
set i = i + 1;
insert into xxx values(….);
until i=n end repeat;
commit;
set autocommit = 1;
end/

当然,这样太死了,全部是固定数据,我们可以来创建function来返回些随机数据:
随机产生数字,范围1~3000(范围可以随意设定,我自身举个列子)
create function rn()
returns smallint
begin
declare res smallint default 0;
set res=floor(1+rand()*3000) ; #这里1和3000的位置可以更改为你需要的范围。
return res;
end/

随机返回指定字符个数,字符范围为大学写字母之间
create function rs(n int)
returns varchar(1024)
begin
declare chars char(52) default ‘abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ’;
declare res varchar(1024) default ”;
declare i int default 0;
repeat
set i = i + 1;
set res = concat(res,substring(chars,floor(1+rand()*52),1));
until i=n end repeat;
return res;
end/
创建完这些function后,我们就可以使用rn()和rs(n)来获取数据数据并insert入测试表了,例如:

create procedure inst(n int)
begin
declare i int default 0;
set autocommit = 0;
repeat
set i = i + 1;
insert into xxx values(rn(),rs(12),rs(7),….);
until i=n end repeat;
commit;
set autocommit = 1;
end/

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息