1

I am debugging a very complicated query on a mature codebase.

Our performance monitoring tool has identified N+1s in a complex part of the codebase that we have assumed to be free of lazy loading.

I would like to temporarily disable (or crash on) lazy loading while debugging certain sections of code.

# In my test suite or while debugging:

PseudoCode.disable_lazy_loading!
SuspectedNPlusOne.run(params) # Crash if lazy loading occurs
PseudoCode.enable_lazy_loading!

How can I disable lazy loading, or temporarily crash on DB reads for the sake of debugging?

1
  • Absolutely impossible ) Commented Apr 12, 2018 at 22:53

1 Answer 1

2

ActiveRecord's querying API revolves around the concept of lazy loading. When I go in to try to clean up N+1 queries I find myself constantly fighting against the way AR was designed to work, which makes it sooo tempting to say "Meh, computers are fast enough".

This is one of several usability reasons that I've decided to switch to Elixir / Phoenix for all future development work; the Ecto ORM makes it literally impossible to lazy load data; you either load an association at query evaluation time or you don't have the associated data, you need to explicitly query it later on.

One very partial suggestion: Install the Bullet gem and use its Bullet.raise = true setting to crash the app if you see any lazy loading. Troubleshooting with Bullet is awkward because it's fighting against ActiveRecord's natural behavior, so it can only tell you where the N+1 query was lazily triggered, not where in the code the query was originally composed (and where you might need to add includes). But at least with this gem in place you can get part of the information you're seeking:

  • Is this code executing obvious N+1 queries?
  • Where in the code was the N+1 query triggered?
  • What class and association name is the culprit?
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.