pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gregory Stark <stark@enterprisedb.com>
To: JS Ubei <jsubei@yahoo.fr>
Cc: pgsql-performance@postgresql.org
Subject: Re: optimize query with a maximum(date) extraction
Date: Wed, 05 Sep 2007 12:30:21 +0100
Message-ID: <87abs1qrhu.fsf@oxford.xeocode.com> (raw)
In-Reply-To: <302443.65402.qm@web26209.mail.ukl.yahoo.com>
References: <302443.65402.qm@web26209.mail.ukl.yahoo.com>

"JS Ubei" <jsubei@yahoo.fr> writes:

> Hi all,
>
> I need to improve a query like :
>
> SELECT id, min(the_date), max(the_date) FROM my_table GROUP BY id;
>
> Stupidly, I create a B-tree index on my_table(the_date), witch is logically
> not used in my query, because it's not with a constant ? isn't it ?

That's not so stupid, it would be possible for a database to make use of such
an index for this query. But it's not one of the plans Postgres knows how to
execute.

I don't think you'll find anything much faster for this particular query. You
could profile running these two (non-standard) queries:

SELECT DISTINCT ON (id) id, the_date AS min_date FROM my_table ORDER BY id, the_date ASC
SELECT DISTINCT ON (id) id, the_date AS max_date FROM my_table ORDER BY id, the_date DESC

I think the first of these can actually use your index but the latter can't
unless you create one for it specifically (which is not so easy -- it'll be
easier in 8.3 though). Worse, I'm not really sure it'll be any faster than the
query you already have.

-- 
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com



view thread (12+ messages)  latest in thread

Message-ID: <87abs1qrhu.fsf@oxford.xeocode.com>
Permalink:  ../87abs1qrhu.fsf@oxford.xeocode.com/
Also on:    postgresql.org/message-id/87abs1qrhu.fsf@oxford.xeocode.com

 · 

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: stark@enterprisedb.com, jsubei@yahoo.fr
  Subject: Re: optimize query with a maximum(date) extraction
  In-Reply-To: <87abs1qrhu.fsf@oxford.xeocode.com>

* 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