Re: nested loop joins

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Greg Rychlewski <greg(dot)rychlewski(at)gmail(dot)com>
Cc: pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Re: nested loop joins
Date: 2021-10-26 18:56:12
Message-ID: 110341.1635274572@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Greg Rychlewski <greg(dot)rychlewski(at)gmail(dot)com> writes:
> I'm looking to understand how nested loop joins with limits work. Say I
> have this query

> SELECT *
> FROM a INNER JOIN b ON a.id = b.id
> LIMIT 10;

> Say this was done as a nested loop join where the rows of "a" are the outer
> loop.

> In this scenario, would all the rows of "a" be materialized or are they
> lazily evaluated?

Evaluation of the join will stop as soon as it's produced 10 rows.
Only as much of "a" will be read as necessary to get that.
(Potentially, not all of "b" would be read either, if 10 matches
to the first "a" row exist in "b".)

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message SQL Padawan 2021-10-27 18:06:07 Re: Strange sequences - how to construct?
Previous Message Greg Rychlewski 2021-10-26 18:47:50 nested loop joins