Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Loading a list and then lazily loading a relation for each row runs one query plus N more; fix it with eager loading such as select_related/prefetch_related or joinedload/selectinload, and catch it with query counting in tests and query monitoring.
ORMs load relationships lazily by default, so iterating over 100 orders and accessing order.customer issues 100 extra queries that are invisible in the code. In Django, select_related uses a JOIN for foreign keys and prefetch_related runs one extra query for many-to-many or reverse relations; in SQLAlchemy, joinedload and selectinload do the same, and lazy='raise' makes accidental lazy loads fail loudly. To prevent it at team level, assert query counts in tests, for example with django_assert_num_queries, use tools like Django Debug Toolbar or nplusone in development, and watch query counts per request in APM. For heavy read paths, explicit queries or annotations that fetch only needed columns can be better than loading full objects.