I'm a junior dev, so sorry if this is dumb.
But we have an issue in production where a query timeout because of a n+1 query, but the same doesn't happen locally.
Is there a way to simulate a production environment locally? I want to know if my fix will actually work before proposing it.
Load a copy of the production database on your local machine.
I’d suggest to use “EXPLAIN ANALYZE” on the actual query on production database and then use same on your new query. You can get query on rails console using “.to_sql”
If you believe it‘s an n+1 issue, you can fix that first and deploy it just because it‘s better than keeping it anyway. If it fixes the slow query, even better.
…you don’t have a staging environment?
Extend the server’s requests timeout. It solves the problem. Trust me
Don’t fix request timeout, fix n+1 queries. Time spent in database depends on mutitude of things, including data volume, load, network latency, machine on which database is run etc. The number of generated queries will be the same on any environment.
So, just list the queries run within a given request and make sure you’re not affected by the n+1 leak.
I can recommend the `bullet` gem for tracing n+1 queries.
Can you share the code with the before and after of your fix?