Skip to content
Advertisement

Avoid N+1 query in large data set

I have a migration which updates existing records with a new attribute value. The model is called ‘MyRecord’. It has millions of records in the database with a new unit_id column of null. I want to update that unit_id column with a specific value:

This creates a lot of N+1 queries:

And some of these N+1 queries are duplicated. I see a lot of this in logs:

I am familiar with eager loading via includes. But when this migration is run to update existing data, there will be no association yet. So I cannot do this:

How can I eliminate the N+1 queries and cache the query so it does not hit database again when s duplicate query?

Advertisement

Answer

Use a simple query, you can think of batching it if it runs for too long:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement