Re: optimizing a join against a windowed function - Mailing list pgsql-general

From David Rowley
Subject Re: optimizing a join against a windowed function
Date
Msg-id CAApHDvpvaf7M=Fz0vHW3TQMX3oco7L11O5Sa3TO3uUF8pHvQnw@mail.gmail.com
Whole thread Raw
In response to optimizing a join against a windowed function  (James Brown <james@instrumentl.com>)
List pgsql-general
On Fri, 30 Aug 2024 at 23:36, James Brown <james@instrumentl.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
oninsights (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
entiregiant 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



pgsql-general by date:

Previous
From: David Rowley
Date:
Subject: Re: Analytic Function Bug
Next
From: "David G. Johnston"
Date:
Subject: Re: default privileges are npt working