Bulk import data into the database

192 views Asked by At

I am using MySQL server as database. I want to import minimum of 10000 records into the database. I found out a gem called activerecord-import which imports data into the database with single query. Is importing bulk data with single query will degrade the performance? Or shall I split those records into group like 2000 records and import it?

1

There are 1 answers

0
Josh Brody On

When you use ActiveRecord to create a new record, it creates a ton of objects under the hood to properly cast each attribute type when you set it on the model, and before you commit it to the database. Creating these objects, and doing their lookups, takes time.

When you execute a query, that takes time to go round-trip.

Having one, large query do a bunch of work takes significant less time.

Will it degrade performance?

Sure, everything has a performance tradeoff, but using something like the gem you mentioned will be a lot less work (and thus performance tradeoff) than going row-by-row.

Don't be afraid to experiment locally and see how everything performs and acts. If/when it comes time to scale, worry about it then.