Fastest Way To Load Data In MySQL


Rails快速批量插入数据的几种方式:

    1:Use transactions(减少 Commit 次数,从而微微的提速,但还是有 N 次数据库的调用)
    2:Get down and dirty with the raw SQL
    3:A single mass insert
    4:Use INSERT statements with multiple VALUES lists with  activerecord-import
    第四种方式是AR方式的70倍左右,已经很快了,还有更快的吗?

  From MySQL Doc:

    When loading a table from a text file, use LOAD DATA INFILE. This is usually 20 times faster than using INSERT statements

SQL语句:

(15955.1ms)  LOAD DATA LOCAL INFILE '/Users/hooopo/data/out/product_sales_facts.txt'
REPLACE INTO TABLE product_sale_facts FIELDS TERMINATED BY ',' (`id`,`date_id`,`order_id`,`product_id`,`address_id`,`unit_price`,`purchase_price`,`gross_profit`,`quantity`,`channel_id`,`gift`)

插入行数:

wc -l < /Users/hooopo/data/out/product_sales_facts.txt
 1271413

大概130w数据用时16秒,平均每秒插入8w条记录左右。如果想用Gem,可以试试 adapter_extensions 和 load_data_infile:

    https://github.com/activewarehouse/adapter_extensions
    https://github.com/EmmanuelOga/load_data_infile
    https://github.com/jsuchal/activerecord-fast-import

MySQL Doc里关于LOAD INFILE的详细介绍:

    http://dev.mysql.com/doc/refman/5.5/en/load-data.html
    http://dev.mysql.com/doc/refman/5.5/en/insert-speed.html


转自:

 http://shopperplus.github.io/blog/2014/11/08/fastest-way-to-load-data-in-mysql.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值