Re: How do I index to speed up OVERLAPS? - Mailing list pgsql-general

From Richard Huxton
Subject Re: How do I index to speed up OVERLAPS?
Date
Msg-id 49351DF1.6060505@archonet.com
Whole thread Raw
In response to How do I index to speed up OVERLAPS?  (Matthew Wilson <matt@tplus1.com>)
List pgsql-general
Matthew Wilson wrote:
> I have a table shift with a start_time column and a stop_time column and
> I do a lot of queries like:
>
>     select * from shift
>     where (start_time, stop_time) overlaps ($A, $B);
>
> $A and $B are user-submitted values.
>
> Anyhow, how do I put indexes on my table to make these queries run
> faster?
>
> Also, is there a better data type to use for ranges of time?

There's not really a good solution for this. A btree or hash index is
pretty much useless for ranges like this (although for certain simple
queries two btree searches can give you a useful subset).

If performance here is really important, you'll need to use some of PG's
 geometric functions. You can treat the range as a box with coordinates
(start,0,end, 1) and then use GiST indexing on the overlaps operation.

--
  Richard Huxton
  Archonet Ltd

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: pg_xlog content
Next
From: Richard Huxton
Date:
Subject: Re: psql verbose mode