Chapter 30 — Quiz
13 questions. Answers at the bottom.
Multiple choice
Q1. An ORM: - A) Replaces the database - B) Maps objects to database rows and generates SQL for you - C) Is a type of index - D) Removes the need to understand SQL
Q2. SQLAlchemy's two layers are: - A) Client and server - B) Core (SQL builder) and ORM (objects) - C) Read and write - D) Sync and async
Q3. You work with ORM objects through a: - A) Cursor - B) Session (unit of work) - C) Pool - D) Trigger
Q4. The N+1 query problem is: - A) One query that returns N rows - B) 1 query for parents + N more (one per parent) to load a relationship in a loop - C) A deadlock - D) A missing index
Q5. The default loading strategy that causes N+1 is: - A) Eager loading - B) Lazy loading (related data fetched on access) - C) Bulk loading - D) No loading
Q6. To fix N+1 in SQLAlchemy, use:
- A) lazyload
- B) selectinload or joinedload (eager loading)
- C) A bigger pool
- D) More indexes only
Q7. ORMs automatically: - A) Make all queries fast - B) Parameterize queries (injection-safe) - C) Remove the need for transactions - D) Denormalize data
Q8. ORMs are best for: - A) Complex multi-table reports with window functions - B) Everyday CRUD and object navigation - C) Bulk loading millions of rows - D) Nothing
Q9. For a complex reporting query, you should usually: - A) Force it through the ORM - B) Drop to Core or raw SQL - C) Avoid the database - D) Denormalize first
Q10. "I use an ORM, so I don't need SQL" is: - A) Correct - B) A dangerous misconception — the ORM hides SQL you must still understand - C) True for reads only - D) True at scale
True/False
Q11. The N+1 problem is usually invisible in small test data and catastrophic in production. (True / False)
Q12. You can run raw SQL through a SQLAlchemy session when the ORM isn't the right tool. (True / False)
Short answer
Q13. A page loops over 1,000 customers and accesses customer.orders for each, and it's slow. Diagnose and fix it.
---
Answer key
Q1 — B. Maps objects↔rows and generates SQL. It does not remove the need to know SQL.
Q2 — B. Core (SQL builder) and ORM (objects).
Q3 — B. A Session.
Q4 — B. 1 + N queries from per-row relationship access.
Q5 — B. Lazy loading (fetch on access).
Q6 — B. Eager loading (selectinload/joinedload).
Q7 — B. They parameterize automatically (injection-safe).
Q8 — B. Everyday CRUD and object navigation.
Q9 — B. Drop to Core/raw SQL.
Q10 — B. Dangerous misconception; the ORM hides SQL you must understand to fix.
Q11 — True. 5 test rows = 6 queries (fine); 50,000 = 50,001 (disaster).
Q12 — True. SQLAlchemy lets you run Core or raw SQL from the same session.
Q13. It's the N+1 problem: 1 query loads the customers, then accessing customer.orders in the loop fires a separate query per customer (~1,000 extra). Diagnose by enabling SQL logging (echo=True) and seeing one query per iteration. Fix with eager loading — select(Customer).options(selectinload(Customer.orders)) — so all orders load in one extra query (2 total), eliminating the per-customer queries.
Scoring: 11–13 you use ORMs well; 8–10 re-drill N+1 and eager loading; below 8, do Exercises B.