N+1 queries: dashboard and invoice list

Source file: evidence/before/n-plus-one.md

Failure mode: performance (FM18 family) · Measured: 2026-11-22T16:05:11.180Z

Impact: opening the dashboard as Maya (Northlake Studio, about 33 invoices) fires 77 API requests and takes 278 ms on localhost with zero network latency. The request count grows by two for every invoice the agency has ever issued.

Method

  • The production build (pnpm before:build) served on 127.0.0.1:4101, the backend (localbase) on 127.0.0.1:4102 with the demo seed.
  • Playwright chromium signs in as maya@northlake.example through the login form, then for each run: open /, reset the backend request log (DELETE /_localbase/log), do a full page load of the target page, wait for the table rows and network idle, read GET /_localbase/log.
  • "Wall time" = last backend response finished minus navigation start. "Sum of server time" = time the backend spent inside the requests. Three runs each; the summary uses the median.
  • Localhost has no network latency. On a real connection each of these sequential requests also pays a round trip (often 50-150 ms to a hosted Supabase region), so the same page takes several seconds.

Dashboard (/dashboard)

RunAPI requestsCORS preflightsTotal HTTPWall time to last responseSum of server time
17743120380 ms139 ms
277077278 ms108 ms
377077262 ms103 ms

Requests in run 1, grouped:

RequestCount
GET /rest/v1/invoice_items35
GET /rest/v1/clients35
GET /rest/v1/memberships2
GET /rest/v1/organizations2
GET /rest/v1/invoices1
GET /rest/v1/projects1
POST /rest/v1/rpc/get_org_revenue1

Invoice list (/invoices)

RunAPI requestsCORS preflightsTotal HTTPWall time to last responseSum of server time
175075258 ms95 ms
27541116500 ms93 ms
375075232 ms85 ms

Requests in run 1, grouped:

RequestCount
GET /rest/v1/invoice_items35
GET /rest/v1/clients35
GET /rest/v1/memberships2
GET /rest/v1/organizations2
GET /rest/v1/invoices1

Why

src/hooks/useInvoices.ts loads every invoice (select=*, no limit, no pagination), then loops over them and awaits two more requests per invoice: invoice_items?invoice_id=eq.<id> and clients?id=eq.<id>. The loop is sequential, so the latency adds up. Totals are then re-summed in the browser from float dollars. CORS preflights (OPTIONS) are counted separately in the tables; in this setup Chromium sent none, so every row above is a real query.

One embedded select (invoices?select=id,number,status,total,client:clients(name)&limit=25) or a server-side endpoint returns the same screen in one request.