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 F3667D1B17B for ; Tue, 29 Jun 2004 04:24:17 -0300 (ADT) 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 68387-08 for ; Tue, 29 Jun 2004 07:24:10 +0000 (GMT) Received: from mpls-qmqp-04.inet.qwest.net (mpls-qmqp-04.inet.qwest.net [63.231.195.115]) by svr1.postgresql.org (Postfix) with SMTP id 99474D1B181 for ; Tue, 29 Jun 2004 04:24:07 -0300 (ADT) Received: (qmail 12788 invoked by uid 0); 29 Jun 2004 07:24:08 -0000 Received: from mpls-pop-12.inet.qwest.net (63.231.195.12) by mpls-qmqp-04.inet.qwest.net with QMQP; 29 Jun 2004 07:24:08 -0000 Received: from 63-227-127-37.dnvr.qwest.net (HELO ?10.0.0.2?) (63.227.127.37) by mpls-pop-12.inet.qwest.net with SMTP; 29 Jun 2004 07:24:08 -0000 Date: Tue, 29 Jun 2004 01:25:03 -0600 Message-Id: <1088493903.12350.5.camel@localhost.localdomain> From: "Scott Marlowe" To: "Harald Lau (Sector-X)" Cc: pgsql-performance@postgresql.org Subject: Re: no index-usage on aggregate-functions? In-Reply-To: <007d01c45da4$3726dcd0$6602a8c0@spock> References: <007d01c45da4$3726dcd0$6602a8c0@spock> Content-Type: text/plain Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200406/282 X-Sequence-Number: 7340 On Tue, 2004-06-29 at 00:42, Harald Lau (Sector-X) wrote: > Hi, > > I've experienced that PG up to current release does not make use of an index when aggregating. Which of course may result in unacceptable answering times > > This behaviour is reproducable on any table with any aggregat function in all of my databases on every machine (PostgreSQL 7.4.2 on i386-redhat-linux-gnu and PostgreSQL 7.2.1 on i686-pc-linux-gnu) > > f.e. querying against a 2.8-mio-records (2.800.000) table the_table > SELECT count(*) FROM the_table > => Seq scan -> takes about 12 sec > > SELECT Avg(num_found) AS NumFound FROM the_table --(index on num_found) > => Seq scan -> takes about 10 sec > > SELECT Sum(num_found) AS TotalFound FROM the_table --(index on num_found) > => Seq scan -> takes about 11 sec > > SELECT Max(date_) AS LatestDate FROM the_table --(index on date_) > => Seq scan -> takes about 14 sec > > But > SELECT date_ AS LatestDate FROM the_table ORDER BY date_ DESC LIMIT 1; > => Index scan -> takes 0.18 msec > > MS SQLServer 2000: Use of an appropriate index _whenever_ aggregating. > > Am I doing something wrong? Yes, you're expecting an MVCC database to behave like a row locking database. Due to the way PostgreSQL is put together, it can't count on an index giving it values, only pointers to values, so to speak. This means it can use an index, but it will still go to the table to get the right value. On the other hand, the trade off is that MVCC can handle much higher parallel loads, usually. Note that if you're aggregate is on sub subset of a table, then an index scan can often be a big win, such as: create table z(a int, b int); insert into z values (1,1); (repeat a couple thousand times) select avg(b) from z where a=3; <-- this can use the index But note that in the above, the table's rows will still have to be accessed to get the right values.