Re: optimizing a join against a windowed function

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: James Brown <james(at)instrumentl(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org, Gabriel Sinkin <gabe(at)instrumentl(dot)com>
Subject: Re: optimizing a join against a windowed function
Date: 2024-08-30 13:27:44
Message-ID: CAApHDvpvaf7M=Fz0vHW3TQMX3oco7L11O5Sa3TO3uUF8pHvQnw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 30 Aug 2024 at 23:36, James Brown <james(at)instrumentl(dot)com> wrote:
> I have two tables: one named taxpayers which has a goodish number of columns an an integer PK id, and one named insights, which has a taxpayer_id foreign key to taxpayers, a year, and (again) a lot of other columns. There's an index on insights (taxpayer_id, year DESC). I'm executing the following SQL:

> If there's only a single value in the IN clause, the EXPLAIN plan looks great:

> However, if there are multiple rows in the IN clause, the optimizer decides to execute the subselect against the entire giant table, and it is not great:

Unfortunately, you've hit a limitation with the EquivalenceClass code.
With the "ON latest_insights.taxpayer_id = taxpayers.id WHERE
taxpayers.id = 650974", the planner is able to deduce that
latest_insights.taxpayer_id is also equal to 650974 and push that
condition down into the common table expression. With the "ON
latest_insights.taxpayer_id = taxpayers.id WHERE taxpayers.id IN (?,
?)" query, the EquivalenceClass code doesn't handle this, so the
optimisation isn't performed. We likely should improve this someday,
but for today, think of it as an unimplemented optimisation rather
than a bug.

> If I add in a second repetitive WHERE clause, it goes back to being happy, but that feels a bit like a hack:

That's likely your best bet on how to make the planner do what you
want, provided you're able to given the query is inside a view.

David

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2024-08-30 13:48:30 Re: default privileges are npt working
Previous Message David Rowley 2024-08-30 13:08:24 Re: Analytic Function Bug