The most common ORM bug
You query 100 users. For each user, you ask for their orders — that's 1 + 100 = 101 queries. Each query is fast in isolation; the round-trip overhead destroys the page. This is the N+1 query problem and it shows up in every ORM, every framework, every team.
How to spot it
Turn on driver-level query logging and watch the count. A page that should make 5 queries making 500 is N+1. Tools like Django Debug Toolbar, Rails Bullet, and SQLAlchemy's events system can flag the pattern automatically.
Three fixes
- Eager loading — fetch related rows in a single JOIN or a single IN-clause. Every ORM has a name for this:
selectinload(SQLAlchemy),prefetch_related(Django),includes(Rails). - Manual JOIN in raw SQL — when ORM eager-loading is awkward, the SQL JOIN is always available.
- Aggregate in SQL, not Python — instead of "for each user, count their orders", use
COUNT(*) ... GROUP BY user_id.