Inserting 10,000 rows? Don't send 10,000 statements.
Loops that insert, update or delete one row per iteration pay a full round trip and a separate statement every time. It is the write-side twin of the N+1 query and just as easy to miss in review.
GreenCode researchers list one query per item among the worst energy wasters in code.
GreenCode helps tackle this by checking a codebase for quality, security and green software patterns in one pass. Row-by-row writes come out as ranked defects, alongside the bugs and security findings. More on this.
Inserting ten thousand rows? Do not send ten thousand statements. Many loops write one row to the database each time round. Each row pays for a full round trip, and its own statement. It is the write-side twin of the N plus one query. And it is just as easy to miss in review. Here is the fix you can use today. Use the bulk form your database library offers. Like executemany, bulk create, COPY, or one insert with many rows. Batch your updates into one statement where the logic allows. Wrap a large batch in one transaction, not one commit per row. Fewer statements mean less work for the database, for the same result. GreenCode researchers list one query per item among the worst energy wasters in code. GreenCode's Constitution skill can help an AI coding agent find and fix these loops. To see them yourself, use tools like Django Debug Toolbar, which lists every query. GreenCode helps tackle this by checking code for quality, security and energy waste together. Row-by-row writes come out as ranked defects, beside the bugs. That was GreenCode Tech Tip number twenty. Follow for the next tip, and find more on greencode.ai/tech-tips.
Share this