On Fri, 30 Aug 2024 at 23:36, James Brown <james@xxxxxxxxxxxxxxx> 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