MySQL插入百万条数据 - 个人总结1

有好多种方法。。。之前也总结了一些,但放到现在来看,效率都一般,于是重新思考总结这个问题;

觉得比较好的方法如下:(推荐方法二

方法一:使用存储过程procedure

每次insert的时候mysql都会自动提交,然后会有其他的一些耗时的操作,所以。。。。。取消掉自动提交不就好了嘛。。。直接 SET AUTOCOMMIT=0;

drop table if exists test;

create table test (c1 int(11) default NULL,c2 varchar(30) default NULL,c3 date default NULL);

drop PROCEDURE if exists test_insert;

delimiter //

CREATE PROCEDURE test_insert(n int)  
    begin  
        declare v int default 0;
        SET AUTOCOMMIT=0;       
        while v < n 
        do  
            insert into test   
            values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));  
            set v = v + 1;  
        end while;
        SET AUTOCOMMIT=1;       
    end //

MySQL插入百万条数据 - 个人总结1_第1张图片

测试结果:

80万:9.67秒;

1000万:3分钟;

100万:大概10多秒左右;

方法二:直接用insert into … select … from …也就是不停地复制表里的数据,再插入

drop table if exists test2;

create table test2 (c1 int(11) default NULL,c2 varchar(30) default NULL);

insert into test2 values(1,'a'),(2,'b'),(3,'c');

insert into test2 select * from test2;

测试结果:

MySQL插入百万条数据 - 个人总结1_第2张图片

MySQL插入百万条数据 - 个人总结1_第3张图片

 

 

参考:

https://blog.csdn.net/qc1110/article/details/70141025?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control

https://blog.csdn.net/u011212394/article/details/90375106

 

你可能感兴趣的:(数据库,mysql,数据库)