Re: Timestamp-based indexing

From: "Harmon S(dot) Nine" <hnine(at)netarx(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Timestamp-based indexing
Date: 2004-07-26 15:42:26
Message-ID: 41052662.3090600@netarx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Thank you for your response :)

This improves the row estimation, but it is still using a sequential scan.

It really seems like the query would go faster if an index scan was
used, given the number of rows fetched (both estimated and actual) is
significantly less than the number of rows in the table.

Is there some way to get the planner to use the timestamp as an index on
these queries?

monitor=# explain analyze select * from "eventtable" where timestamp
between (CURRENT_TIMESTAMP - INTERVAL '10 min') AND CURRENT_TIMESTAMP;

QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Seq Scan on "eventtable" (cost=0.00..23103.29 rows=2047 width=155)
(actual time=10227.253..10276.944 rows=1662 loops=1)
Filter: ((("timestamp")::timestamp with time zone >=
(('now'::text)::timestamp(6) with time zone - '@ 10 mins'::interval))
AND (("timestamp")::timestamp with time zone <=
('now'::text)::timestamp(6) with time zone))
Total runtime: 10278.628 ms
(3 rows)

monitor=# SELECT COUNT(*) FROM "eventtable";
count
--------
425602
(1 row)

monitor=#

-- Harmon

Kevin Barnard wrote:

>
>
> Harmon S. Nine wrote:
>
>> monitor=# explain analyze select * from "eventtable" where timestamp
>> > CURRENT_TIMESTAMP - INTERVAL '10 minutes';
>> QUERY PLAN
>
>
> Try
>
> SELECT * FROM eventtable where timestamp BETWEEN (CURRENT_TIMESTAMP -
> INTERVAL '10 minutes') AND CURRENT_TIMESTAMP;
>
> This should will use a range off valid times. What your query is
> doing is looking for 10 minutes ago to an infinate future. Statically
> speaking that should encompass most of the table because you have an
> infinate range. No index will be used. If you assign a range the
> planner can fiqure out what you are looking for.
>

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Harmon S. Nine 2004-07-26 15:46:42 Re: Timestamp-based indexing
Previous Message Matthew T. O'Connor 2004-07-26 15:09:53 Re: Timestamp-based indexing