>>>> Daniele Varrazzo <[EMAIL PROTECTED]> wrote: > >> select count(*) from foo >> where foo.account_id in ( >> select id from accounts where system = 'abc'); > >> Total runtime: 13412.226 ms > > Out of curiosity, how does it do with the logically equivalent?: > > select count(*) from foo > where exists (select * from accounts > where accounts.id = foo.account_id > and accounts.system = 'abc');
I tried it: it is slower and the query plan still includes the seqscan: Aggregate (cost=44212346.30..44212346.31 rows=1 width=0) (actual time=21510.468..21510.469 rows=1 loops=1) -> Seq Scan on foo (cost=0.00..44205704.40 rows=2656760 width=0) (actual time=0.058..21402.752 rows=92790 loops=1) Filter: (subplan) SubPlan -> Index Scan using accounts_pkey on accounts (cost=0.00..8.27 rows=1 width=288) (actual time=0.002..0.002 rows=0 loops=5313519) Index Cond: (id = $0) Filter: (("system")::text = 'abc'::text) Total runtime: 21510.531 ms Here the estimate is even more gross: 2656760 is exactly the 50% of the records in the table. -- Daniele Varrazzo - Develer S.r.l. http://www.develer.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance