I run a query where I am looking for a record, and another record after a while.
Table definition:
(
id integer primary key,
gpsstatus character(2),
datetime timestamp without time zone,
lat numeric(9,6),
lon numeric(9,6),
alt numeric(9,4),
time integer,
datafileid integer,
shape geometry,
speed double precision,
dist double precision,
shape_utm geometry,
lokalitet character(128),
cowid integer
)
There are indices in datetime, lokalitet, cowid, gpsstatus, gist-index in shape and shape_utm.
Points should be sampled every 5 seconds, so I tried to do
select <something more>,p1.timestamp
from table p1, table p2
where p1.timestamp + interval '5 secound' = p2.timestamp
This went fast enough, but then I found out that I had lost quite a lot of points due to jitter in the sample, so the points could be 4 to 6 seconds apart.
Then I tried:
where (p2.timestamp, interval'0 second')
overlaps (p1.timestamp + interval '4 second', interval '2 second')
and it took forever. I also tried a simpler solution:
WHERE p1.timestamp + interval '4 second' <= p2.timestamp
AND p1.timestamp + interval '6 second' >= p2.timestamp
which also turned out to be unusually slow.
The timestamp field has a normal index. Is there a special type of index - is it something else that will make this query usable?
Request at the moment:
SELECT
p1.cowid,
p1.datetime,
st_distance(p1.shape_utm, lead(p1.shape_utm)
OVER (ORDER BY p1.datetime)) AS meters_obs,
st_distance(p1.shape_utm, lead(p1.shape_utm, 720)
OVER (ORDER BY p1.datetime)) AS meters_hour,
observation.observation
FROM (gpspoint p1 LEFT JOIN observation
ON (observation.gpspointid = p1.id)),
status
WHERE p1.gpsstatus = status.id
AND status.use = true;
, .