Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Checkpoint: Performance

โญโญโญ advancedโณ 30 min read๐Ÿ“ Lesson 134 of 169

Kill the N+1: batch a per-item fetch into at most 2 queries, proven by a query counter.

Checkpoint โ€” the N+1 rescue

db records every db.query(sql, params) call. The naive implementation makes one query PER user (N+1). Batch it:

  • fetch_user_orders(user_ids, db) returns {user_id: [{'id':โ€ฆ, 'total':โ€ฆ}, โ€ฆ]}
  • with at most 2 queries total (regardless of user count)
  • order lists per user keep the row order returned by the db

This is the exact shape you will meet again in the database module โ€” learned here as a performance pattern, applied there as SQL.