Why distinct so slow ?

From: Hervé Piedvache <footcow(at)noos(dot)fr>
To: pgsql-performance(at)postgresql(dot)org
Subject: Why distinct so slow ?
Date: 2004-11-15 21:22:02
Message-ID: 200411152222.02601.footcow@noos.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi,

I have a strange (for me) result ... Why the second request is really quicker
with a Seq Scan than the first one with a DISTINCT and using an index !?

The table have really 183957 rows ... not like the Seq Scan seems to
expect ... !? I understand nothing here ...

Thanks for your explanations ...

# explain analyze SELECT distinct s.id_category FROM site s;

QUERY
PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Unique (cost=0.00..11903.15 rows=56 width=4) (actual time=0.147..1679.170
rows=68 loops=1)
-> Index Scan using ix_site_id_category on site s (cost=0.00..11496.38
rows=162706 width=4) (actual time=0.143..1452.611 rows=183957 loops=1)
Total runtime: 1679.496 ms
(3 rows)

Time: 1680,810 ms

# explain analyze SELECT s.id_category FROM site s group by id_category;

QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=7307.83..7307.83 rows=56 width=4) (actual
time=1198.968..1199.084 rows=68 loops=1)
-> Seq Scan on site s (cost=0.00..6901.06 rows=162706 width=4) (actual
time=0.097..921.676 rows=183957 loops=1)
Total runtime: 1199.260 ms
(3 rows)

--
Bill Footcow

Browse pgsql-performance by date

  From Date Subject
Next Message Allen Landsidel 2004-11-15 22:22:36 Re: Strange (?) Index behavior?
Previous Message Josh Berkus 2004-11-15 21:03:37 Re: Question on pgsql optimisation of SQL and structure (index, etc)