Chapter 30 — Further Reading

SQLAlchemy (💻 Developer)

  • SQLAlchemy Unified Tutorial (https://docs.sqlalchemy.org/en/20/tutorial/) — the modern (2.0) tutorial covering Core and ORM together. The best starting point.
  • SQLAlchemy ORM: "Relationship Loading Techniques." The authoritative guide to lazy vs. eager loading — selectinload, joinedload, subqueryload — i.e., how to avoid N+1 (Case Study 1).
  • SQLAlchemy: "Working with Engines and Connections" / text() — running raw SQL when the ORM isn't the tool (Case Study 2).

The N+1 problem (everyone using an ORM)

  • "The N+1 query problem" explainers (framework-agnostic) — it bites Django, Rails, Hibernate, and SQLAlchemy alike. Recognize it everywhere.
  • SQL echo / query loggingcreate_engine(echo=True), plus APM tools (e.g., query counters) that flag N+1 automatically.

ORM judgment (💻 Developer · 🏗️ DBA)

  • "When to use an ORM vs. raw SQL" essays — the balanced view (Case Study 2's lesson). ORMs for CRUD, SQL for analytics.
  • "ORMs are a leaky abstraction" discussions — why understanding the generated SQL is non-negotiable.
  • Alembic (https://alembic.sqlalchemy.org/) — SQLAlchemy's migration tool; autogenerates migrations from model changes (ties to Chapter 22).

Other ORMs (context)

  • Django ORM, Rails Active Record, Hibernate (Java), Entity Framework (.NET) — the same concepts (mapping, lazy/eager loading, N+1) in other ecosystems. The skills transfer.

Reference (this book)

  • Chapter 29 — psycopg2: the raw layer under the ORM (parameterization, transactions, pooling).
  • Chapter 22 — Migrations: Alembic for schema evolution.
  • Chapters 7, 11 — Aggregation & CTEs: the SQL you'll reach for when the ORM isn't the tool.

Do, don't just read

  • Reproduce the N+1 (Case Study 1) with echo=True: loop over customers accessing orders, count the queries, then fix with selectinload and re-count.
  • Write one analytical report both ways — ORM expression vs. raw SQL — and compare clarity and generated SQL (Case Study 2).
  • Always run with echo=True while learning so the SQL the ORM generates is never a mystery.

Next: Chapter 31 — Bulk Data and ETL: moving data at scale with COPY.