slow SELECT ... LIMIT query - Mailing list pgsql-general

From Simon Stanlake
Subject slow SELECT ... LIMIT query
Date
Msg-id 000801c10fb1$fe6bf300$6400a8c0@localdomain
Whole thread Raw
Responses Re: slow SELECT ... LIMIT query  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: slow SELECT ... LIMIT query  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
Hi,

I have a fairly large table (1 million records) with the following
structure...

sampleid int4
unitid int4
datetimestamp timestamp
data1 float8
data2 float8

btree indexes on sampleid, unitid, and datetimestamp.

I want to be able to pull out the most recent record for a certain unit.

the query that seemed most reasonable was
SELECT * FROM MYTABLE WHERE UNITID = unit_id ORDER BY DATETIMESTAMP DESC
LIMIT 1;

some strange results...

1) for units that have a lot of records (100K), the query is reasonably fast
(~ 0.5 seconds) but for units with not too many records (100) the query is
REALLY SLOW, like 15 seconds.  the explain plan says INDEX SCAN BACKWARDS so
I guess it is using the index.

2) when I take away the LIMIT 1 and run the query it returns the results
almost immediately no matter how many records the unit has.

It looks like the indexes are not being used properly when the LIMIT clause
is inserted.  I didn't do anything fancy when I created the indexes, just
the standard CREATE INDEX statement.  Ran VACUUM ANALYZE and everything.

Is this common when you use the LIMIT clause?  Is there a work around?

Thanks,
Simon


pgsql-general by date:

Previous
From: will trillich
Date:
Subject: Re: psql -l
Next
From: "Debra LaVille"
Date:
Subject: Intermittent Startup Failures