pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Graham Davis <gdavis@refractions.net>
To: Chris Browne <cbbrowne@acm.org>
Cc: pgsql-performance@postgresql.org
Subject: Re: BUG #2658: Query not using index
Date: Tue, 03 Oct 2006 13:32:24 -0700
Message-ID: <4522C8D8.4010601@refractions.net> (raw)
In-Reply-To: <60d599gqcz.fsf@dba2.int.libertyrms.com>
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>

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.
>  
>





view thread (17+ messages)  latest in thread

Message-ID: <4522C8D8.4010601@refractions.net>
Permalink:  ../4522C8D8.4010601@refractions.net/
Also on:    postgresql.org/message-id/4522C8D8.4010601@refractions.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: gdavis@refractions.net, cbbrowne@acm.org
  Subject: Re: BUG #2658: Query not using index
  In-Reply-To: <4522C8D8.4010601@refractions.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox