mysql 怎么运行的 测试大批量插入数据

86 阅读1分钟


delimiter ;;

CREATE PROCEDURE insertData(in sum INT) 
BEGIN 
DECLARE count INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
set count=0;
set i = rand() * 10000;
set @exesql = concat("insert into single_table(id,key1,key2,key3, key_part1, key_part2,key_part3, common_field) values ");
set @exedata = "";
-- 随机姓名 可根据需要增加/减少样本
set @SURNAME = 'abcdefghijkrqeyrqerpeqwreqwfqewrr';
 
set @NAME = 'abcdefghijkrqeyrqerpeqwreqwfqewrrfadsfopdasfmalsdfklasdfkldakslfadmsfadfasdfa';


while count<sum do 
-- length(@surname)/3 是因为中文字符占用3个长度
set @key1 = concat(substr(@surname,floor(rand()*length(@surname)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1));
set @key3 = concat(substr(@surname,floor(rand()*length(@surname)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1));
set @key_part1 = concat(substr(@surname,floor(rand()*length(@surname)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1));
set @key_part2 = concat(substr(@surname,floor(rand()*length(@surname)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1));
set @key_part3 = concat(substr(@surname,floor(rand()*length(@surname)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1));
set @common_field = concat(substr(@surname,floor(rand()*length(@surname)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1), substr(@NAME,floor(rand()*length(@NAME)/3+1),1));
    set @exedata = concat(@exedata, ",('", i, "','", @key1, "','", i+1,"','", @key3,"','", @key_part1,"','", @key_part2,"','", @key_part3,"','", @common_field, "')");
    set count=count+1;
    set i=i+1;
    if i%1000=0
    then 
        set @exedata = SUBSTRING(@exedata, 2);
        set @exesql = concat("insert into single_table(id,key1,key2,key3, key_part1, key_part2,key_part3, common_field) values ", @exedata);
        prepare stmt from @exesql;
        execute stmt;
        DEALLOCATE prepare stmt;
        set @exedata = "";
    end if;
end while;

if length(@exedata)>0 
then 
    set @exedata = SUBSTRING(@exedata, 2);
    set @exesql = concat("insert into single_table(id,key1,key2,key3, key_part1, key_part2,key_part3, common_field) values ", @exedata);
    prepare stmt from @exesql;
    execute stmt;
    DEALLOCATE prepare stmt;
end if;

end;


delimiter ;