Skip to main content

N+1 queries, ORM eager loading and connection pooling

Explain how ORMs cause N+1 queries, compare joinedload vs selectinload, and explain PgBouncer.

Official Reference

Model Answer & Explanation

Lazy-loading a relationship inside a loop over N parent rows fires 1 + N SQL queries. In SQLAlchemy, selectinload issues one clean WHERE parent_id IN (...) query (best for 1-to-many without duplicating parent rows), while joinedload uses a JOIN (good for many-to-1). Use PgBouncer in transaction pooling mode because Postgres forks a heavy backend process per connection.

Follow-up Interview Question · What breaks under PgBouncer transaction pooling if your app uses prepared statements or session-level SET?

Module Context & References

PostgreSQL & Redis · Popular interview theory High-yield indexing, MVCC, query plan and Redis caching questions
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.