I have a query:
select *
from tableA
where colA > (select max(colA) from tableB)
This is terribly slow compared to:
select *
from tableA
where colA > 10
There are indices on tableA and tableB on colA.
If it matters, this problem occurs in Postgres, a db-independent solution would be nice.
query plan:
Seq Scan on "tableA" a (cost=0.47..145224.62 rows=1256724 width=178)
Filter: (colA > $1)
InitPlan 2 (returns $1)
-> Result (cost=0.46..0.47 rows=1 width=8)
InitPlan 1 (returns $0)
-> Limit (cost=0.43..0.46 rows=1 width=8)
-> Index Only Scan using idx_colA_desc on "tableB" b (cost=0.43..106076.56 rows=3770169 width=8)
Index Cond: (colA IS NOT NULL)
indeces:
CREATE INDEX ON tableA USING btree (colA DESC)
CREATE INDEX ON tableB USING btree (colA DESC)