Re: query with timestamp not using index

From: Richard Huxton <dev(at)archonet(dot)com>
To: Brian Hirt <bhirt(at)mobygames(dot)com>
Cc: 'Postgresql Performance' <pgsql-performance(at)postgresql(dot)org>
Subject: Re: query with timestamp not using index
Date: 2004-12-01 17:38:28
Message-ID: 41AE0194.9050705@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Brian Hirt wrote:
> I have a query that fetches information from a log, based on an indexed
> column. The timestamp in the table is with time zone, and the server
> time zone is not GMT. However, i want all of the timestamps for a
> particular day in GMT. If i simply use a date constant, the index is
> used, but the incorrect rows are fetched, since the date is converted
> to a timestamp in the server's time zone. When i cast that date to a
> GMT date, the index is no longer used. Is there some better way to
> write the query so that the planner will use the index? I have
> simplied the queries below to demonstrate the problem i'm having.
> Thanks for any advice.
>
>
> SLOW:
> basement=# select count(*) from redir_log
> basement-# where redir_timestamp >= '10/14/2004'::timestamp without
> time zone at time zone 'GMT';

Not quite what's wanted. Try keeping things as a timestamp with timezone
(you can add a timestamp to a date):

SELECT count(*) FROM redir_log
WHERE redir_timestamp BETWEEN '2004-10-14+00'::timestamptz AND
CURRENT_TIMESTAMP;

Putting two bounds on the range can also help index usage.

In actual fact, since you're comparing to a timestamp and not a date,
I'd personally supply a valid timestamptz: '2004-10-14 00:00:00+00'
--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2004-12-01 20:06:58 Re: query with timestamp not using index
Previous Message Brian Hirt 2004-12-01 16:46:43 query with timestamp not using index