I have a fairly large table (25m entries) which stores tracking data, I am not a postgres admin by trade so this may be a simple problem.
these two queries although similar are taking massively different amounts of time and I am not sure why.
SELECT MAX(logdatetime) FROM tlog WHERE pointid = 'and.andrew' (5100ms)
SELECT MAX(logdatetime) FROM tlog WHERE pointid = '359710040139645' (41ms)
logdatetime is indexed, and I ran a VACUUM
and REINDEX
on the table when I noticed the discrepancy, but it hasn't resolved the issue.
Here is the EXPLAIN ANALYZE
output for each of the queries:
--- and.andrew
Result (cost=35.20..35.21 rows=1 width=0) (actual time=1924.811..1924.811 rows=1 loops=1)
InitPlan 1 (returns $0)
-> Limit (cost=0.00..35.20 rows=1 width=8) (actual time=1924.804..1924.804 rows=1 loops=1)
-> Index Scan Backward using logdatetime on tlog (cost=0.00..2434879.14 rows=69168 width=8) (actual time=1924.800..1924.800 rows=1 loops=1)
Index Cond: (logdatetime IS NOT NULL)
Filter: ((pointid)::text = 'and.andrew'::text)
Total runtime: 1924.853 ms
--- 359710040139645
Result (cost=1.44..1.45 rows=1 width=0) (actual time=32.525..32.525 rows=1 loops=1)
InitPlan 1 (returns $0)
-> Limit (cost=0.00..1.44 rows=1 width=8) (actual time=32.518..32.518 rows=1 loops=1)
-> Index Scan Backward using logdatetime on tlog (cost=0.00..2434879.14 rows=1688018 width=8) (actual time=32.513..32.513 rows=1 loops=1)
Index Cond: (logdatetime IS NOT NULL)
Filter: ((pointid)::text = '359710040139645'::text)
Total runtime: 32.568 ms
The column pointid
is defined as character varying (50)
, in case that matters.
pointid='and.andrew'
versus the other? – voretaq7 Oct 25 '12 at 23:24