pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ezequiel Tolnay <mail@etolnay.com.ar>
To: pgsql-sql@postgresql.org
Subject: Re: Trimming the cost of ORDER BY in a simple query
Date: Tue, 03 May 2005 18:08:23 +1000
Message-ID: <d57bhc$2nfr$1@news.hub.org> (raw)
In-Reply-To: <1115104938.5853.25.camel@localhost.localdomain>
References: <20050503043531.23516.qmail@web31106.mail.mud.yahoo.com>
	<1115104938.5853.25.camel@localhost.localdomain>

Ragnar Hafstaư wrote:
> another possibility (total guess) is a functional index
> create index func_id_date on user_url(user_id,(add_date::date));
> SELECT ... ORDER BY uu.user_id DESC ,uu.add_date::date DESC LIMIT 7;
> I have no idea if this will work, or if the planner will use such
> an index.
To make sure the index is used you could build the query using a 
subquery like this:

SELECT DISTINCT date_part('year',  add_date),  date_part('month', 
add_date),  date_part('day', add_date)
FROM (
  SELECT uu.add_date::date as add_date
  FROM user_url uu
  GROUP BY uu.user_id, uu.add_date
  HAVING uu.user_id=1
  ORDER BY uu.add_date::date DESC
  LIMIT 7
) x;

Perhaps a select distinct instead of the group by in the subquery would 
make use the index?

Cheers,

Ezequiel Tolnay



view thread (5+ messages)  latest in thread

Message-ID: <d57bhc$2nfr$1@news.hub.org>
Permalink:  ../d57bhc$2nfr$1@news.hub.org/
Also on:    postgresql.org/message-id/d57bhc$2nfr$1@news.hub.org

 · 

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-sql@postgresql.org
  Cc: mail@etolnay.com.ar
  Subject: Re: Trimming the cost of ORDER BY in a simple query
  In-Reply-To: <d57bhc$2nfr$1@news.hub.org>

* 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