Re: query with timestamp not using index

Поиск
Список
Период
Сортировка
От Richard Huxton
Тема Re: query with timestamp not using index
Дата
Msg-id 41AE0194.9050705@archonet.com
обсуждение исходный текст
Ответ на query with timestamp not using index  (Brian Hirt <bhirt@mobygames.com>)
Список 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

В списке pgsql-performance по дате отправления:

Предыдущее
От: Brian Hirt
Дата:
Сообщение: query with timestamp not using index
Следующее
От: Brian Hirt
Дата:
Сообщение: Re: query with timestamp not using index