kb/data/en.wikipedia.org/wiki/Nested_loop_join-0.md

190 lines
3.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Nested loop join"
chunk: 1/1
source: "https://en.wikipedia.org/wiki/Nested_loop_join"
category: "reference"
tags: "science, encyclopedia"
date_saved: "2026-05-05T11:36:41.498435+00:00"
instance: "kb-cron"
---
A nested loop join is a naive algorithm that joins two relations by using two nested loops. Join operations are important for database management.
== Algorithm ==
Two relations
R
{\displaystyle R}
and
S
{\displaystyle S}
are joined as follows:
algorithm nested_loop_join is
for each tuple r in R do
for each tuple s in S do
if r and s satisfy the join condition then
yield tuple <r,s>
This algorithm will involve nr*bs+ br block transfers and nr+br seeks, where br and bs are number of blocks in relations R and S respectively, and nr is the number of tuples in relation R.
The algorithm runs in
O
(
|
R
|
|
S
|
)
{\displaystyle O(|R||S|)}
I/Os, where
|
R
|
{\displaystyle |R|}
and
|
S
|
{\displaystyle |S|}
is the number of tuples contained in
R
{\displaystyle R}
and
S
{\displaystyle S}
respectively and can easily be generalized to join any number of relations ...
The block nested loop join algorithm is a generalization of the simple nested loops algorithm that takes advantage of additional memory to reduce the number of times that the
S
{\displaystyle S}
relation is scanned. It loads large chunks of relation R into main memory. For each chunk, it scans S and evaluates the join condition on all tuple pairs, currently in memory. This reduces the number of times S is scanned to once per chunk.
== Index join variation ==
If the inner relation has an index on the attributes used in the join, then the naive nest loop join can be replaced with an index join.
algorithm index_join is
for each tuple r in R do
for each tuple s in S in the index lookup do
yield tuple <r,s>
The time complexity for this variation improves from
O
(
|
R
|
|
S
|
)
to
O
(
|
R
|
log
|
S
|
)
{\displaystyle O(|R||S|){\text{ to }}O(|R|\log |S|)}
== See also ==
Hash join
Sort-merge join
== References ==