SELECT timeseries_id, "timestamp" FROM enhydris_timeseriesrecord WHERE timeseries_id=6661 ORDER BY "timestamp" DESC LIMIT 1;
(The table contains about 66m records, and the ones with timeseries_id=6661 are about 0.5m.)
This query takes about 1-2 seconds to run, which I find too much.
If it was using a simple btree index, it should locate what it's looking for after about 30 iterations. As far as I can see when I execute EXPLAIN ANALYZE
for that query, it does use the index, but it has to do so in each chunk, and apparently there are 1374 chunks.
How can the query become faster?
Table "public.enhydris_timeseriesrecord"
Column | Type | Collation | Nullable | Default
---------------+--------------------------+-----------+----------+---------
timeseries_id | integer | | not null |
timestamp | timestamp with time zone | | not null |
value | double precision | | |
flags | character varying(237) | | not null |
Indexes:
"enhydris_timeseriesrecord_pk" PRIMARY KEY, btree (timeseries_id, "timestamp")
"enhydris_timeseriesrecord_timeseries_id_idx" btree (timeseries_id)
"enhydris_timeseriesrecord_timestamp_idx" btree ("timestamp" DESC)
"enhydris_timeseriesrecord_timestamp_timeseries_id_idx" btree ("timestamp", timeseries_id)
Foreign-key constraints:
"enhydris_timeseriesrecord_timeseries_fk" FOREIGN KEY (timeseries_id) REFERENCES enhydris_timeseries(id) DEFERRABLE INITIALLY DEFERRED
Triggers:
ts_insert_blocker BEFORE INSERT ON enhydris_timeseriesrecord FOR EACH ROW EXECUTE PROCEDURE _timescaledb_internal.insert_blocker()
Number of child tables: 1374 (Use \d+ to list them.)
Update: EXPLAIN plan