N+1 queries
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Layer 3 · Go deeper · 6 min

    Go deeper

    Size it, build it, then make the calls an interviewer will push you on.

    Build-up · 4 steps

    From a staircase of queries to two

    The same 50-restaurant page, traced twice. Drawn to show order, not to scale.

    Step 1 of 4

    One query for the list

    SELECT … LIMIT 50 returns 50 restaurants with their dishes not yet loaded. So far, one round trip.

    4 interview moments

    You’re the candidate

    The interviewer pushes on the hard parts. Pick an approach and see how it holds up.

    1. Deep dive 1 of 4

      Interviewer

      “A Django page lists 50 restaurants, each with its zone and its dishes. How do you load them?”

      Your move. Pick an approach:

    2. Deep dive 2 of 4

      Interviewer

      “A GraphQL query asks for 50 restaurants and each one’s brand. The brand resolver runs one query per call. What do you do?”

      Your move. Pick an approach:

    3. Deep dive 3 of 4

      Interviewer

      “The feed service calls the user service once per author, 50 calls a page. How do you fix it?”

      Your move. Pick an approach:

    4. Deep dive 4 of 4

      Interviewer

      “An order page lazy-loads its 3 delivery addresses, so 4 queries. A reviewer says fix the N+1. Do you?”

      Your move. Pick an approach:

    Build-up · 4 steps

    How DataLoader batches one tick

    A GraphQL query asks for 50 restaurants and each one’s brand. The brand resolver hardly changes; the batching happens underneath it.

    Step 1 of 4

    Fifty resolvers ask

    The brand resolver runs once per restaurant. Each call now returns a promise from brandLoader.load(id) instead of sending a query.