Ich glaube, ich habe es endlich:
select * from view1 where view1.id = ANY(( select array(select ext_id from aggregate_table order by somedata limit 10) )::integer[]) order by view1.somedata; Aufbauend auf der Beobachtung von @Dukeling:
I suspect where id in (1,2,3,4,5,6,7,8,9,10) can be optimised and where id in (select ...) can't, the reason being that (1,2,3,4,5,6,7,8,9,10) is a constant expression, while the select is not.
und nachdem ich dies im schnelleren Plan gesehen hatte:
Recheck Cond: (id = ANY ('{1,2,3,4,5,6,7,8,9,10}'::integer[])) Index Cond: (id = ANY ('{1,2,3,4,5,6,7,8,9,10}'::integer[])) funktioniert die Array-Form – und sie ist tatsächlich schneller als die erste Abfrage in der Frage, etwa 1,2 ms. Der Plan zeigt jetzt:
Recheck Cond: (id = ANY ($1)) Index Cond: (id = ANY ($1)) plus Bitmap-Scans, die die Unterabfrage-Form nie erzeugt hat. Die Erkenntnis: Das Zusammenfassen der Unterabfrage zu einem einzelnen Array-Wert gibt dem Planner etwas, das er als eine Einheit behandeln und worauf er einen Index anwenden kann.