efficient bulk update rails database

后端 未结 5 920
借酒劲吻你
借酒劲吻你 2020-12-25 10:15

I\'m trying to build a rake utility that will update my database every so often.

This is the code I have so far:

namespace :utils do

  # utils:updat         


        
相关标签:
5条回答
  • 2020-12-25 10:43

    As Larry says, use your DB-specific import utilities if the file comes in the format you want. However, if you need to manipulate the data before inserting, you can generate a single INSERT query with data for many rows, which is faster than using a separate query for each row (as ActiveRecord will do). For example:

    INSERT INTO iptocs (ip_from, ip_to, country_code) VALUES
      ('xxx', 'xxx', 'xxx'),
      ('yyy', 'yyy', 'yyy'),
      ...;
    
    0 讨论(0)
  • 2020-12-25 10:44

    You could generate a text file with all INSERTs you need and then execute:

    mysql -u user -p db_name < mytextfile.txt
    

    Not sure if this will be any faster but worth a try...

    0 讨论(0)
  • 2020-12-25 10:45

    Use the database level utilities for high speed Luke!

    Unfortunately, they're db specific. But they are fast For mysql, see http://dev.mysql.com/doc/refman/5.1/en/load-data.html

    0 讨论(0)
  • 2020-12-25 10:47

    I'm currently experimenting with activerecord-import, which sounds very promising:

    https://github.com/zdennis/activerecord-import

    0 讨论(0)
  • 2020-12-25 10:52

    Have you tried to use AR Extensions for bulk import? You get impressive performance improvements when you are inserting 1000's of rows to DB. Visit their website for more details.

    Refer to these examples for more information

    Usage Example 1

    Usage Example 2

    Usage Example 3

    0 讨论(0)
提交回复
热议问题