I really love Rails, but it’s not the most performant code in the world. Though it doesn’t often arise in CRUD programming, if you do any sort of stats, ML, or data analytics, you’ll frequently find yourself wanting to import lots of data into your db. You could create an ActiveRecord object for each row, but this is glacial, requiring one round trip to the db server per row, and is likely to abuse the kindness of your dba. Instead, there is a wonderful gem called ar-extensions
that allows you to access mysql’s native bulk import facilities. To use it you just call Model.import
with arrays of data and their corresponding fields. For example, say I have a table like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
This has a corresponding model AdsenseAnalyticsDay
. Batch importing with rails is then trivial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
where the csv looks like:
1 2 3 4 5 6 7 8 9 10 11 12 |
|