AI Glossary / Data engineering and architecture / Advanced
Nested Loop
Definition
A nested loop is a process in which an inner operation runs for each item handled by an outer operation. In this glossary's database context, it refers to a nested-loop join: for each row from one input, the system examines the other input for matching rows. A suitable index can make the inner search selective. The term names an execution strategy, not the business relationship used to decide which records match.
What it means
Imagine checking a short list of selected courses against a larger list of registrations. A basic implementation takes one course, searches the registrations, then repeats for the next course. PostgreSQL describes nested-loop joins in these terms and notes that an index on the inner side can make them useful. The presence of a nested loop in a plan is therefore not automatically a performance defect. The number of outer rows and the cost of each inner search are central to understanding the work performed.
How it works
The join receives an outer row and supplies its relevant values to the inner operation. Matching pairs are returned according to the query's condition. A simple implementation scans every inner row each time; an indexed implementation can seek matching candidates instead. PostgreSQL EXPLAIN shows the chosen plan, while EXPLAIN ANALYZE reports actual execution information, including repeated loops. Those observations help distinguish a large repeated scan from a small repeated lookup. The loop structure does not itself change the required matches, and the full cost includes accessing records, evaluating conditions and delivering the result.
Three practical examples
- A reporting query joins a small set of selected courses to indexed registration records and performs an inner lookup for each selected course.
- A data engineer investigates an unexpectedly slow AI-generated query whose inner scan repeats far more often than estimated.
- A teaching exercise compares the pair checks in a basic nested-loop join with the smaller set of pairs that actually satisfy its condition.
Think of it this way
Imagine a librarian holding three request slips. For each slip, the librarian searches a catalog for the requested title. Reading the entire catalog three times resembles a simple repeated scan. Using a title index for each slip resembles an indexed lookup. Both follow the same outer-request, inner-search pattern, but their effort can differ considerably. The analogy does not imply that an index makes every search free or every workload faster.
Worked example
Consider an original fictional equality inner join. The outer input has three course identifiers: C1, C2 and C3. The inner input has four registrations: R1 for C1, R2 for C1, R3 for C2 and R4 for C4. A basic nested loop that tests every pair performs 3 × 4 = 12 equality checks. It returns three matching pairs: C1-R1, C1-R2 and C2-R3. C3 has no match, and R4 has no selected course. If an appropriate inner index is used, the system can instead perform three keyed searches and retrieve the matching candidates. This does not mean the total work becomes exactly three comparisons: index navigation, record access and other checks still have costs. The example assumes no early termination, no additional filters and no null identifiers. It counts logical pair checks for the stated basic algorithm, not disk reads or elapsed milliseconds.
Illustrative case study
In a fictional AI course dashboard, a query initially asks about three selected courses and performs acceptably. A later prompt requests all courses, greatly increasing the outer input. The same general repeated-search pattern now does much more work. Reviewers compare estimated and observed row counts, inspect the inner access path and measure the complete query with representative data. They also verify that any revised plan returns the same course-registration relationships. The scenario illustrates why a useful strategy for a small selection may need reassessment for a broader request. It does not claim that a hash join always wins, or that disabling nested loops is an appropriate universal fix.
Common misconceptions and mistakes
A nested loop does not necessarily rescan an entire physical table for every outer row; the inner operation can use a selective access path. Conversely, the existence of an index does not establish that the chosen plan uses it effectively. Do not equate pair comparisons with result rows: the toy example checks twelve pairs but returns three. Another mistake is treating a plan's estimated cost as a measured duration. Finally, a nested-loop join is not the same as a nested SQL subquery. Query syntax and the optimizer's execution strategy are different levels of description.
Business use
For an AI analytics feature, test representative narrow and broad questions rather than relying on a single quick demonstration. In the fictional dashboard, a request for three courses and a request for the entire catalog may produce very different amounts of repeated work. Record the outer row count, inner access pattern and actual end-to-end time. Investigate substantial estimation errors before drawing conclusions from a plan label. A useful improvement preserves the intended answer and addresses the observed workload, including the cost of returning large results to the user.
People also ask
Are nested-loop joins always slow?
No. A small outer input with efficient inner lookups can make the strategy appropriate.
Does a twelve-pair comparison produce twelve output rows?
Only if all twelve pairs satisfy the relevant join condition in the stated inner-join setting.
Is a nested query guaranteed to use a nested-loop join?
No. The optimizer determines execution strategies from the query and available plans.
Check your understanding
How many pair checks does the basic toy loop perform?
Answer: Twelve.
How many matching rows does it return?
Answer: Three.
Why is C3 absent from that result?
Answer: It has no matching registration in the stated inner join.
Why are three indexed searches not necessarily three total comparisons?
Answer: Each search can involve navigation and candidate checking.
Which change increases repeated inner work?
Answer: Increasing the number of outer rows, all else being equal.
Related terms
- Hash join
- Join algorithm
- index scan
- merge join
- query plan
- cardinality estimate
Learn next
Write two short lists and enumerate every pair tested by a basic nested loop. Mark the matching pairs separately. Then describe what information an index would need to locate candidates directly, and explain which costs your paper calculation leaves out.
More in Data engineering and architecture
- Natural Join - A natural join combines rows from two relations by matching the values of all attributes whose names occur in both inputs, retaining one copy of each shared...
- Natural key - A natural key identifies a database record using attributes that already have meaning in the business domain.
- Navigational database - A navigational database is a database accessed by following defined paths between related records.
- Nested Relation - A nested relation is a relation whose tuples can contain relation-valued attributes: a value can itself be a collection of structured records.
- Network Data Model - The network data model organizes database records through explicitly defined relationships that can form a network rather than a single tree.
- Network on a chip - A network on a chip, usually written network-on-chip or NoC, is a communication network inside an integrated circuit.
Sources
PostgreSQL documentation, Planner/Optimizer: https://www.postgresql.org/docs/current/planner-optimizer.html PostgreSQL documentation, Using EXPLAIN: https://www.postgresql.org/docs/current/using-explain.html The course-registration arithmetic, librarian analogy and AI-dashboard scenario are original educational illustrations. No measured speedup is claimed from the comparison count.
Previous: Nested hypothesis Next: Nested partition model