Re: Wasteful nested loop join when there is `limit` in the query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: WU Yan <4wuyan(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Wasteful nested loop join when there is `limit` in the query
Date: 2025-02-17 07:01:41
Message-ID: 744079.1739775701@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

WU Yan <4wuyan(at)gmail(dot)com> writes:
> Hello everyone, I am still learning postgres planner and performance
> optimization, so please kindly point out if I missed something obvious.

An index on employee.name would likely help here. Even if we had
an optimization for pushing LIMIT down through a join (which you
are right, we don't) it could not push the LIMIT through a sort step.
So you need presorted output from the scan of "employee". I think
this example would behave better with that. You may also need to
test with non-toy amounts of data to get the plan you think is
better: an example with only half a dozen rows is going to be
swamped by startup costs.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message 馬 騰飛 2025-02-17 07:55:01 The performance issues caused by upgrading PostgreSQL to version 16.3.
Previous Message WU Yan 2025-02-17 06:38:49 Wasteful nested loop join when there is `limit` in the query