Hi,
I can see in the postgresql logs that certain simple queries (no joins and using only match conditions that use indexes) take anywhere from 1 to 3 seconds to execute. I log queries that take longer than a second to execute thus there are similar queries which execute under a second which don't get reported.
When I try the same query using EXPLAIN ANALYZE, it takes a few milliseconds.
The table contains around 8 million records and is written to and queried extensively. I have enabled auto vacuum and even recently (few hours ago) ran a VACUUM ANALYZE on that table.
Sample query log entry: Dec 30 10:14:57 db01 postgres[7400]: [20-1] LOG: duration: 3857.322 ms statement: SELECT * FROM "responses" WHERE ("responses".contest_id = 17469) AND (user_id is not Dec 30 10:14:57 db01 postgres[7400]: [20-2] null) ORDER BY updated_on desc LIMIT 5
contest_id and user_id is indexed. updated_on is not indexed. If I index it, the query planner ignores the contest_id index ans uses updated_on instead which further slows down the query. The maximum entries on the above query without the LIMIT would not be more than 1000.
Any help would be much appreciated.