Re: Select "todays" timestamps in an index friendly way - Mailing list pgsql-general

From Steven Lembark
Subject Re: Select "todays" timestamps in an index friendly way
Date
Msg-id 20181029075841.3db03113.lembark@wrkhors.com
Whole thread Raw
In response to Select "todays" timestamps in an index friendly way  ("Lutz Horn" <lutz.horn@posteo.de>)
List pgsql-general
>     create temporary table t (
>       id SERIAL primary key,
>       ts timestamp not null default now()
>     );

* add date( ts ) as a field and index date = now()::date.

* Depending on the amount of data in your table the date
  may not be seletive enough to be worth using, at which 
  point the index may be present and ignored. Only way to
  be sure is analyze it.

* Might be worth looking at a partial index using >= 00:00:00 
  and < 24:00:00 (PG grocks the 2400 notation for "midnight at 
  the end of today) or

    where ts && tsrange( ... 00:00:00, ... 24:00:00, '[)] )

  Nice thing about the partial index is that you can create it
  on all of the non-ts fields for fast lookup by whatever and 
  only index the portion for today. 

* Think about using a materialized view rather than a temp
  table. May prove simpler to query.


-- 
Steven Lembark                                     3920 10th Ave South
Workhorse Computing                               Birmingham, AL 35222
lembark@wrkhors.com                                    +1 888 359 3508


pgsql-general by date:

Previous
From: "Sakai, Teppei"
Date:
Subject: RE: Which index is used in the index scan.
Next
From: Diego Andres Ruiz Gomez
Date:
Subject: Research survey