X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1BE2A3A4221 for ; Mon, 15 Nov 2004 21:23:48 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 55362-09 for ; Mon, 15 Nov 2004 21:23:37 +0000 (GMT) Received: from pm-mx3.mx.noos.fr (pm-mx3.mgn.net [195.46.220.211]) by svr1.postgresql.org (Postfix) with ESMTP id BF5E93A4A91 for ; Mon, 15 Nov 2004 21:23:38 +0000 (GMT) Received: from s249.dhcp212-198-144.noos.fr (s249.dhcp212-198-144.noos.fr [212.198.144.249]) by pm-mx3.mx.noos.fr (Postfix) with ESMTP id 5767026F18 for ; Mon, 15 Nov 2004 22:22:03 +0100 (MET) From: =?iso-8859-15?q?Herv=E9_Piedvache?= To: pgsql-performance@postgresql.org Subject: Why distinct so slow ? Date: Mon, 15 Nov 2004 22:22:02 +0100 User-Agent: KMail/1.7 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200411152222.02601.footcow@noos.fr> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/214 X-Sequence-Number: 9168 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