---
title: "Tech Tip #1: Stop the N+1 query"
description: "Your page makes a hundred trips to the database when one would do."
url: https://greencode.ai/tech-tips/1-stop-the-n-plus-one-query/
---

# Tech Tip #1: Stop the N+1 query

**Your page makes a hundred trips to the database when one would do.**

[Watch the video](https://media.greencode.ai/shorts/stop-the-n-plus-one-query-d7dc2d7af9ee.mp4)

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

### The problem

Code fetches a list, then asks the database again for every item on it. A hundred items means a hundred and one round trips, each with its own connection work and query planning. Every query is fast on its own, so the pattern hides in plain sight.

### The fix you can use today

- Fetch the related rows in one go: a join, or one batched lookup with IN (...).
- Ask your ORM to load related data eagerly (select\_related / prefetch\_related, JOIN FETCH, include).
- Check every loop that has a database call inside it.

GreenCode researchers list the N+1 query 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
- [Scaphandre](https://github.com/hubblo-org/scaphandre?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode): measures the power drawn by each process on a Linux server and exports it to Prometheus and other monitoring stacks

### How GreenCode helps

GreenCode helps tackle this by checking code for quality, security and energy waste in one pass. Wasteful calls like these 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#loops

### Sources

- [GreenCode Constitution (Kapenekakis et al., GreenAI workshop, ECML PKDD 2026): N+1 query patterns in Tier 1, the critical energy anti-patterns](https://greencode.ai/publications/greencode-constitution-agent-principles-for-optimizing-code-and-llm-inference-en/)
- [GreenCode Constitution skill, energy anti-patterns: C1 N+1 query patterns](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)
- [W3C, Web Sustainability Guidelines: retrieve data in as few queries as possible, filter in the database](https://www.w3.org/TR/web-sustainability-guidelines/#efficient-database-querying)

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

#### Key Facts

- Tip **#1**
- For **Developers**
- Category **Databases, APIs and storage**
- Format **short video**
![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/)
