Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id EF64B9FB254 for ; Tue, 3 Oct 2006 17:34:48 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 27633-07 for ; Tue, 3 Oct 2006 17:34:44 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.refractions.net (mail.refractions.net [24.68.236.214]) by postgresql.org (Postfix) with ESMTP id 3836A9FB20B for ; Tue, 3 Oct 2006 17:34:44 -0300 (ADT) Received: from lion.animals (lion [192.168.50.200]) by mail.refractions.net (Postfix) with ESMTP id 9F816122C4FD; Tue, 3 Oct 2006 13:42:54 -0700 (PDT) Received: by lion.animals (Postfix, from userid 88) id 2F642E3D4; Tue, 3 Oct 2006 13:34:43 -0700 (PDT) Received: from refractions.net (unknown [192.168.50.43]) by lion.animals (Postfix) with ESMTP id 4A083E3BD; Tue, 3 Oct 2006 13:34:04 -0700 (PDT) Message-ID: <4522C8D8.4010601@refractions.net> Date: Tue, 03 Oct 2006 13:32:24 -0700 From: Graham Davis Reply-To: gdavis@refractions.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Chris Browne Cc: pgsql-performance@postgresql.org Subject: Re: BUG #2658: Query not using index References: <200609272056.k8RKuWjY040283@wwwmaster.postgresql.org> <20061003000147.GA31475@wolff.to> <4522AA01.4020709@refractions.net> <60irj1gr3r.fsf@dba2.int.libertyrms.com> <4522B5B9.2050206@refractions.net> <60d599gqcz.fsf@dba2.int.libertyrms.com> In-Reply-To: <60d599gqcz.fsf@dba2.int.libertyrms.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.1 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200610/55 X-Sequence-Number: 21184 How come an aggreate like that has to use a sequential scan? I know that PostgreSQL use to have to do a sequential scan for all aggregates, but there was support added to version 8 so that aggregates would take advantage of indexes. This is why SELECT max(ts) AS ts FROM asset_positions; Uses an index on the ts column and only takes 50 milliseconds. When I added the group by it would not use a multikey index or any other index. Is there just no support for aggregates to use multikey indexes? Sorry to be so pushy, but I just want to make sure I understand why the above query can use an index and the following can't: SELECT assetid, max(ts) AS ts FROM asset_positions GROUP BY assetid; -- Graham Davis Refractions Research Inc. gdavis@refractions.net Chris Browne wrote: >gdavis@refractions.net (Graham Davis) writes: > > >>40 seconds is much too slow for this query to run and I'm assuming >>that the use of an index will make it much faster (as seen when I >>removed the GROUP BY clause). Any tips? >> >> > >Assumptions are dangerous things. > >An aggregate like this has *got to* scan the entire table, and given >that that is the case, an index scan is NOT optimal; a seq scan is. > >An index scan is just going to be slower. > >