From: Ragnar Hafstaư <gnari@simnet.is>
To: ogjunk-pgjedan@yahoo.com
Cc: pgsql-sql@postgresql.org
Subject: Re: Trimming the cost of ORDER BY in a simple query
Date: Tue, 03 May 2005 07:22:18 +0000
Message-ID: <1115104938.5853.25.camel@localhost.localdomain> (raw)
In-Reply-To: <20050503043531.23516.qmail@web31106.mail.mud.yahoo.com>
References: <20050503043531.23516.qmail@web31106.mail.mud.yahoo.com>
On Mon, 2005-05-02 at 21:35 -0700, ogjunk-pgjedan@YAHOO.COM wrote:
> Query (shows the last 7 dates):
>
> => SELECT DISTINCT date_part('year', uu.add_date), date_part('month',
> uu.add_date), date_part('day', uu.add_date) FROM user_url uu WHERE
> uu.user_id=1 ORDER BY date_part('year', uu.add_date) DESC,
> date_part('month', uu.add_date) DESC, date_part('day', uu.add_date)
> DESC LIMIT 7;
>
i assume add_date is a timestamp, because otherwise you could
ORDER by add_date, and use an index on (desc, add_date):
SELECT DISTINCT .... WHERE uu.user_id=1
ORDER BY uu.user_id DESC ,uu.add_date DESC LIMIT 7;
this will not work for a timestamp add_date
> QUERY PLAN:
...
> Total runtime: 20.313 ms
20 ms does not sound like a big problem.
if 20 ms is really too much for you, and add_date IS a timestamp,
you might think about adding a date column , maintained
by triggers of by your application, and add an index on (user_id, x)
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.
gnari
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: gnari@simnet.is, ogjunk-pgjedan@yahoo.com
Subject: Re: Trimming the cost of ORDER BY in a simple query
In-Reply-To: <1115104938.5853.25.camel@localhost.localdomain>
* 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