Re: An "obvious" index not being used

From: "Daniele Varrazzo" <piro(at)develer(dot)com>
To: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: An "obvious" index not being used
Date: 2008-06-19 14:03:38
Message-ID: 36573.82.111.147.97.1213884218.squirrel@www.develer.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance


>>>> Daniele Varrazzo <piro(at)develer(dot)com> 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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Lionel 2008-06-20 10:53:45 Re: Which hardware ?
Previous Message Kevin Grittner 2008-06-19 13:46:26 Re: An "obvious" index not being used