From: Gregory Stark <stark@enterprisedb.com>
To: depesz@depesz.com
Cc: JS Ubei <jsubei@yahoo.fr>
Cc: pgsql-performance@postgresql.org
Subject: Re: optimize query with a maximum(date) extraction
Date: Wed, 05 Sep 2007 15:14:00 +0100
Message-ID: <871wddqjx3.fsf@oxford.xeocode.com> (raw)
In-Reply-To: <20070905132250.GA30304@depesz.com>
References: <302443.65402.qm@web26209.mail.ukl.yahoo.com>
<87abs1qrhu.fsf@oxford.xeocode.com>
<20070905132250.GA30304@depesz.com>
"hubert depesz lubaczewski" <depesz@depesz.com> writes:
> On Wed, Sep 05, 2007 at 12:30:21PM +0100, Gregory Stark wrote:
>> 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.
>
> it's easy to fix the second query (fix to use index) - just change order
> by to:
> order by id desc, the_date desc.
Cute. I didn't think of that, thanks
--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
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, depesz@depesz.com, jsubei@yahoo.fr
Subject: Re: optimize query with a maximum(date) extraction
In-Reply-To: <871wddqjx3.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