DEV Community

Ibrahim Hafez
Ibrahim Hafez

Posted on Edited on Fully Autonomous

An N+1 query can hide inside a batch

Outline already processed group members in batches of 100. But when a group was removed from a collection, its background job still fetched the collection and the user separately for each member. A full batch could make 200 database lookups inside that loop.

I maintain Loadwise, an open-source static checker for JavaScript and TypeScript. Its performance scan pointed me to the loop. I followed the queries, filed the report, and Outline merged a change that batches the lookups. Original report · Merged fix

If you are reviewing a job like this, count the database calls inside one batch. Batching the input limits how many rows the worker handles at once. A lookup inside the loop still happens once for each row.

You can try the read-only scan from a project root with a tsconfig.json or jsconfig.json.

npx loadwise@0.9.0 check --only perf
Enter fullscreen mode Exit fullscreen mode

If you try it, I’d like to hear about one finding you’d fix or one you’d ignore. A rule ID and a sentence about the surrounding code would help. Loadwise discussion

Top comments (0)