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 EDC9D329D76 for ; Sun, 3 Oct 2004 16:27:08 +0100 (BST) 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 85945-05 for ; Sun, 3 Oct 2004 15:26:51 +0000 (GMT) Received: from boutiquenumerique.com (gailleton-2-82-67-9-10.fbx.proxad.net [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id E4EB5329E2C for ; Sun, 3 Oct 2004 16:26:49 +0100 (BST) Received: (qmail 8856 invoked from network); 3 Oct 2004 17:27:12 +0200 Received: from unknown (HELO musicbox) (192.168.0.2) by gailleton-2-82-67-9-10.fbx.proxad.net with SMTP; 3 Oct 2004 17:27:12 +0200 To: Shiar , pgsql-performance@postgresql.org Subject: Re: index not used when using function References: <20040929194131.GH22917@shiar.org> Message-ID: From: =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?= Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Sun, 03 Oct 2004 17:29:37 +0200 In-Reply-To: <20040929194131.GH22917@shiar.org> User-Agent: Opera M2/7.53 (Linux, build 737) 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: 200410/27 X-Sequence-Number: 8503 Maybe add an order by artist to force a groupaggregate ? > Hi all, a small question: > > I've got this table "songs" and an index on column artist. Since > there's about > one distinct artist for every 10 rows, it would be nice if it could use > this > index when counting artists. It doesn't however: > > lyrics=> EXPLAIN ANALYZE SELECT count(DISTINCT artist) FROM songs; > Aggregate (cost=31961.26..31961.26 rows=1 width=14) (actual > time=808.863..808.864 rows=1 loops=1) > -> Seq Scan on songs (cost=0.00..31950.41 rows=4341 width=14) > (actual time=26.801..607.172 rows=25207 loops=1) > Total runtime: 809.106 ms > > Even with enable_seqscan to off, it just can't seem to use the index. > The same > query without the count() works just fine: > > lyrics=> EXPLAIN ANALYZE SELECT DISTINCT artist FROM songs; > Unique (cost=0.00..10814.96 rows=828 width=14) (actual > time=0.029..132.903 rows=3280 loops=1) > -> Index Scan using songs_artist_key on songs (cost=0.00..10804.11 > rows=4341 width=14) (actual time=0.027..103.448 rows=25207 loops=1) > Total runtime: 135.697 ms > > Of course I can just take the number of rows from the latter query, but > I'm > still wondering why it can't use indexes with functions. > > Thanks