douhuan1497 2013-03-18 10:33
浏览 156
已采纳

将海量数据插入Mysql数据库的最快方法[重复]

This question already has an answer here:

I actually have a list of 100,000 records which I'd like to insert into the MySQL database.

I have tried to insert them with foreach and simple INSERT INTO however it took a lot of time to insert even 100 row. Like 1 second / row.

IS there any method to insert these rows much faster?

</div>
  • 写回答

6条回答 默认 最新

  • dor65412 2013-03-18 10:49
    关注

    Using one INSERT statement with multiple rows is faster than one INSERT statement per row. This will reduce calls to the database.

    Example:

     INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doujing3896 2013-03-18 10:39
    关注

    Make packages:

    INSERT INTO `table_name`(`column`) VALUES('value'),VALUES('value1'),VALUES('value2'), ..., VALUES('valuen');
    

    documentation for insert

    or export data to csv or other text format and use LOAD DATA, look here: load data by mysql client

    评论
  • douduoyan5943 2013-03-18 10:40
    关注

    fastest way to load large dataset is to use bulk loading interface. Refer Mysql docs: Bulk loader

    评论
  • dounuo8797 2013-03-18 10:51
    关注

    The fastest way to use command line interface. You can use mysqldump util.

    评论
  • dr2898 2013-03-18 11:00
    关注

    Group several similar INSERTs in one long INSERT with multiple VALUES lists to insert several rows at a time: query will be quicker due to fact that connection + sending + parsing a query takes 5-7 times of actual data insertion (depending on row size). If that is not possible, use START TRANSACTION and COMMIT, if your database is InnoDB, otherwise use LOCK TABLES — this benefits performance because the index buffer is flushed to disk only once, after all INSERT statements have completed; in this case unlock your tables each 1000 rows or so to allow other threads access to the table.

    From this article: http://www.ajaxline.com/node/2099

    评论
  • drktvjp713333 2017-09-14 13:45
    关注

    (only for mysql)

    This makes a function to return random names. you can change it to fit yourself

    CREATE FUNCTION `get_name`() RETURNS varchar(5) CHARSET utf8
    BEGIN
        DECLARE chars varchar(255) default '里对非肺乔额及爱我动物地位三次幂动物';
        DECLARE str varchar(255) default '';
        DECLARE i int default 0;
        while i<3 do
            set str=concat(str,substring(chars, FLOOR(1 + RAND()*62), 1));
            set i=i+1;
        END while;
    RETURN str;
    END
    

    make a procedure to achieve making lots of data

    CREATE PROCEDURE `make_data`(size int)
    BEGIN
        declare i int default 0;
        while i<size do
            insert table_name(name)value(get_name());
        end while;
    END
    

    call make_data(1000)[done]

    If you use MySQL Workbench, you need to add function and procedure manually. If you use MySQL console, then you should add DELIMITER like below

    DELIMITER $$
    $$
    CREATE PROCEDURE `make_data`(size int)
    BEGIN
        declare i int default 0;
        while i<size+1 do
            insert table_name(name)value(get_name());
        end while;
    END$$
    
    DELIMITER ;
    
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 arcgis制做交通拥堵时变图
  • ¥15 AD20 PCB板步线 这个要怎么步啊
  • ¥50 关于《奇迹世界》1.5版本,数据修改
  • ¥15 请问这个问题如何解决(关键词-File)
  • ¥50 visual studio 2022和EasyX图形化界面
  • ¥15 找一下报错原因,纠正一下
  • ¥50 Cox回归模型Nomogram图制作报错
  • ¥20 SQL如何查询多级用户的数据
  • ¥15 给车牌识别代码加一个识别轮廓长宽比的代码
  • ¥30 商品价格预测的transformer模型优化