Menu

An N+1 query can hide inside a batch
📰

An N+1 query can hide inside a batch

DEV Community: node·Ibrahim Hafez·about 9 hours ago
#zFQ0nBGj
#devtonodejs#devto#dev#inside#loop
Reading 0:00
15s threshold

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 [email protected] 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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.