---
title: "Tech Tip #20: Write in bulk, not row by row (video)"
description: "Inserting 10,000 rows? Don't send 10,000 statements."
url: https://greencode.ai/shorts/20-bulk-writes-not-row-by-row/
---

# Tech Tip #20: Write in bulk, not row by row (video)

## Tech Tip #20: Write in bulk, not row by row

**Inserting 10,000 rows? Don't send 10,000 statements.**

[Watch the video](https://media.greencode.ai/shorts/bulk-writes-not-row-by-row-bb5eda465bb0.mp4)

Video created by [explainapaper.com](https://explainapaper.com/?utm_source=greencode.ai&utm_medium=film&utm_campaign=made-by)

### The problem

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.

### The fix you can use today

- Use the bulk form your driver offers: executemany(), bulk\_create(), COPY or a multi-row INSERT.
- Batch updates with one statement and a WHERE ... IN (...) where the logic allows.
- Wrap a large batch in one transaction instead of committing per row.

GreenCode researchers list one query per item among the worst energy wasters in code.

### Tools that help

- [GreenCode Constitution skill](https://greencode-constitution.org/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (GreenCode researchers): open-source energy principles and a catalogue of 37 energy anti-patterns that an AI coding agent can follow to find and fix waste like this
- [Django Debug Toolbar](https://django-debug-toolbar.readthedocs.io/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode): lists every SQL query a request made, so repeated queries in a loop stand out

### How GreenCode helps

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](https://greencode.ai/feature/sustainability-led-code-quality-assessment/).

### Tags

#sql#orm#databases#performance

### Sources

- [GreenCode Constitution (Kapenekakis et al., GreenAI workshop, ECML PKDD 2026): query-per-item loops among the Tier 1 anti-patterns](https://greencode.ai/publications/greencode-constitution-agent-principles-for-optimizing-code-and-llm-inference-en/)
- [GreenCode Constitution skill, energy anti-patterns: C8 query-per-item loops: bulk operations such as IN (...), executemany(), bulk\_create()](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)

### Transcript

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.

[The tip in full](https://greencode.ai/tech-tips/20-bulk-writes-not-row-by-row/)

[« Tech Tip #19: Make AI pay for itself in energy](https://greencode.ai/shorts/19-ai-must-pay-for-itself/)[Tech Tip #21: Find and remove zombie resources »](https://greencode.ai/shorts/21-remove-zombie-resources/)

[All GreenCode Tech Tips](https://greencode.ai/tech-tips/) · [All GreenCode videos](https://greencode.ai/videos/)

#### Key Facts

- Tip **#20**
- For **Developers**
- Length **1:15**
- Category **Databases, APIs and storage**
![GreenCode](https://greencode.ai/wp-content/uploads/2024/12/greencode-logo-website-small-colours-2.webp)

Want this checked on your own system? Talk to the GreenCode team.

[Get in touch](https://greencode.ai/contact/)
