简体   繁体   English

Mysql性能问题:插入10000条记录时超时,创建5000条记录仍然需要26秒

[英]Mysql performance issue: it times out when inserting 10000 records and still take 26 seconds creating 5000 records

How can I improve the performance of the below procedure? 如何改善以下程序的性能? What is causing the large delay? 造成大延迟的原因是什么?

I am using a intel core i5 8th gen chip with 16 Gb of RAM and Mysql 8.0 community edition (default configuration). 我正在使用带有16 Gb RAM和Mysql 8.0社区版(默认配置)的intel核心i5第8代芯片。 The below procedure times out when I set NUMROWS to 10000 and still takes 26 seconds when 5000 is set. 当我将NUMROWS设置为10000时,以下过程超时,设置5000时仍需要26秒。

        delimiter //

        create procedure rand_data()
        begin
            declare NUMROWS int default 5000;
            declare count int default 0;
        declare fullemail varchar(40) default null;
        declare dateregistered date default null;
        declare activated bool default false;

        drop table if exists users;
        create table users (id int primary key auto_increment, email varchar(40) not null, registered date not null, active bool default false);

        while count < NUMROWS do
            set fullemail := concat('user',count,'name@email.com');
            set dateregistered := date(now() - interval round(10000*rand()) day);
        set activated := round(rand());
        insert into users(email, registered, active) values (fullemail, dateregistered, activated);
        set count := count + 1;
    end while;
end//

delimiter ;

One alternative could be to do a massive insert creating a String and executing when the loop is over 一种替代方法可以是创建一个大型插入,创建一个String并在循环结束时执行

CREATE DEFINER=`test`@`%` PROCEDURE `rand_data`()
begin
        declare NUMROWS int default 10000;
        declare count int default 0;
    declare fullemail varchar(40) default null;
    declare dateregistered date default null;
    declare activated bool default false;

    set @query = ' insert into users(email, registered, active) values ';

    drop table if exists users;
    create table users (id int primary key auto_increment, email varchar(40) not null, registered date not null, active bool default false);

    while count < NUMROWS do
        set fullemail := concat('user',count,'name@email.com');
        set dateregistered := date(now() - interval round(10000*rand()) day);
        set activated := round(rand());

        set @query = CONCAT(@query, '(''',fullemail,''', ''',dateregistered,''', ',activated,'),');

        set count := count + 1;
    end while;

    set @query = SUBSTRING(@query, 1, CHAR_LENGTH(@query)-1);
    set @query = CONCAT(@query,';');

    PREPARE stmt FROM @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt; 
end

I hope this works for you. 我希望这适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM