Need help regarding query performance in PostgreSQL. This seems to apply to indices.
This request:
- Filters according to
type - Orders
timestamp , ascending:
SELECT * FROM the_table WHERE type = 'some_type' ORDER BY timestamp LIMIT 20
Indices:
CREATE INDEX the_table_timestamp_index ON the_table(timestamp); CREATE INDEX the_table_type_index ON the_table(type);
type field values ββare just one of about 11 different lines.
The problem is that the query seems to run in O (log n) time, but only takes a few milliseconds more, except for some type values ββthat run in a few minutes.
In these sample queries, only a few milliseconds are first executed, and the second takes more than 30 minutes:
SELECT * FROM the_table WHERE type = 'goq' ORDER BY timestamp LIMIT 20 SELECT * FROM the_table WHERE type = 'csp' ORDER BY timestamp LIMIT 20
I suspect, with approximately 90% certainty, that our indices are incorrect. I think, after reading this similar question about index performance , that most likely we need a composite index, over type and timestamp .
The query plans I completed are here:
Many thanks for your help! Any pointers would be really appreciated!
Juan Carlos Coto
source share