October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Slaying the N+1 Query Dragon: A Practical Guide to Database Optimization

N+1 queries happen when an application fetches related data one parent at a time. Learn how to spot the pattern and choose an ORM loading strategy that fits your workload.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The N+1 query problem occurs when an application fetches a set of records, then issues one additional database query for each record to load related data. To fix it, make relationship loading intentional: fetch needed data up front or project only the fields you need, then inspect the generated SQL and measure the result. A single query is not automatically faster; joins and separate queries have different costs.

What is the N+1 query problem?

Suppose an application loads a list of blogs with one query. It then loops through the blogs and reads each blog’s posts. If the ORM lazily loads posts when that property is accessed, the application sends another query for each blog: one initial query plus N follow-up queries for N blogs. Microsoft describes this pattern as capable of causing “very significant performance issues.” Microsoft’s EF Core performance guide shows the behavior in practice.

As an Amazon Associate I earn from qualifying purchases.

The extra cost is not just the SQL work. Each query can require a network roundtrip, so a seemingly ordinary property read inside a loop can become a series of database calls. ORMs may hide those calls behind familiar-looking object access, which makes the pattern easy to miss.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I fix N+1 queries?

Start from the data the caller actually needs. If the related data is known in advance, load it deliberately with an eager-loading strategy. If the caller needs only a few values, project those values rather than retrieving whole related objects. These approaches avoid the per-parent query pattern without requiring every relationship to be loaded for every operation.

  1. Find the repeated access. Look for a parent-record query followed by relationship access inside a loop, template, serializer, or response-building step.
  2. Choose the response shape. Decide which related records and fields are actually needed. Do not load every relationship by default.
  3. Select a loading strategy. Use eager loading or a projection where appropriate; reserve explicit or lazy loading for cases where a separate, later fetch is intentional.
  4. Inspect the SQL. Confirm the number of statements, joins, selected columns, and returned rows. An ORM method name alone does not tell you the database work it generated.
  5. Measure the real workload. Compare latency, database execution, memory use, and consistency needs with representative data and application conditions.

EF Core describes eager loading as fetching related data as part of the initial query, explicit loading as requesting it later with a separate query, and lazy loading as fetching it transparently when a navigation property is accessed. Its documentation cautions that lazy loading can create unnecessary roundtrips. See EF Core’s guide to loading related data and its lazy-loading guidance.

How the main ORMs handle relationship loading

EF Core: Include, projection, and split queries

For a known response shape, EF Core can use Include to load a relationship or a projection to select only the needed values. Projection can reduce unnecessary columns and avoid materializing more object data than the caller uses. When eager-loading multiple collections would produce excessive duplicated joined rows, compare split queries rather than assuming one joined statement is best. Check the EF Core version and database provider because exact behavior can vary.

EF Core’s single-versus-split query guidance notes that split queries add roundtrips, may require buffering, and can observe inconsistent data if records change between statements. Those costs matter alongside the risk of repeated parent data in a large join.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

SQLAlchemy: select-in and joined loading

SQLAlchemy 2.1 documents lazy loading as a common source of N+1 SELECTs. selectinload() issues additional SELECT statements using parent identifiers in an IN clause; it is not necessarily one SQL statement, but it replaces a query per parent with a more controlled loading pattern. joinedload() uses a JOIN in the main statement. The documentation describes select-in loading as generally simple and efficient for collections, and joined loading as a general-purpose choice for many-to-one relationships.

raiseload() can make unexpected relationship access fail instead of silently issuing a lazy-load query, helping expose accidental database calls during development. Select-in loading can be affected by composite primary keys and backend support. Consult the SQLAlchemy 2.1 relationship-loading documentation for the precise behavior of the version and database in use.

Django: select_related and prefetch_related

Django’s select_related() joins related fields into the SQL SELECT. prefetch_related() runs separate relationship lookups and combines the results in Python. They are different strategies, not interchangeable spellings for “eager load”; choose according to the relationship and inspect the queries they produce. See the Django QuerySet API reference.

Hibernate: choose a fetch strategy deliberately

Hibernate’s guide describes the same pattern: one query loads a list, then additional queries load associated instances. Hibernate offers multiple association-fetching strategies to avoid it, but the right API and configuration depend on the Hibernate version and mapping. See the Hibernate 7.1 guide and verify version-specific details for your project.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When is a join better than separate queries?

There is no universally fastest loading strategy. A join can reduce statement count and roundtrips, but it can repeat parent columns for each matching child row. Joining multiple collections can multiply rows further, creating a cartesian expansion. Separate or split queries can reduce duplicated result data, but they make more roundtrips and may require extra buffering or create consistency concerns if data changes between statements.

Compare the options against the workload rather than optimizing query count in isolation:

  • Statements and roundtrips: How many database calls are made, and how costly is each roundtrip in this deployment?
  • Rows and duplication: Does a join repeat parent data, or do multiple collections expand the result substantially?
  • Data selected: Are the returned columns and relationships all needed by the caller?
  • Database work: Is the SQL manageable, and what does the database execution plan show?
  • Memory and buffering: Can the application handle the result size, particularly for large collections?
  • Consistency: Must related values reflect one consistent view, or can the data change between multiple statements?
  • Relationship and backend constraints: Does the chosen loading method fit the relationship cardinality and the database’s capabilities?

Use representative data and measure both application latency and database behavior. A lower query count may still mean more rows, more transferred data, or greater memory use; a few controlled statements can be preferable to one oversized join.

Why is my ORM making so many database queries?

Look for relationship access that triggers a query on demand, especially inside loops or code that renders or serializes a collection of parent records. Then confirm the pattern in generated SQL or query logs: an initial parent query followed by repeated statements with changing parent identifiers is a strong sign of N+1 behavior. Also check whether the application is loading data it does not need. Fix the access pattern, then re-check the SQL and measure the changed workload.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.