๐ 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.