Create an index on a continuous aggregate
Adding an index to your continuous aggregate can make your analytical queries lightning fast
Continuous aggregates create helpful composite indexes by default, but you can turn that off or add your own for specific filters. This page covers the automatic indexes, how to disable them, and how to manage custom ones.
Automatically created indexes
Section titled “Automatically created indexes”When you create a continuous aggregate, an index is automatically created for
each GROUP BY column. The index is a composite index, combining the GROUP BY
column with the time_bucket column.
For example, if you define a continuous aggregate view with GROUP BY device, location, bucket, two composite indexes are created: one on {device, bucket}
and one on {location, bucket}.
Turn off automatic index creation
Section titled “Turn off automatic index creation”To turn off automatic index creation, set timescaledb.create_group_indexes to
false when you create the continuous aggregate.
For example:
CREATE MATERIALIZED VIEW conditions_daily WITH (timescaledb.continuous, timescaledb.create_group_indexes=false) AS ...Manually create and drop indexes
Section titled “Manually create and drop indexes”You can use a regular PostgreSQL statement to create or drop an index on a continuous aggregate.
For example, to create an index on avg_temp for a materialized hypertable
named weather_daily:
CREATE INDEX avg_temp_idx ON weather_daily (avg_temp);Indexes are created under the _timescaledb_internal schema, where the
continuous aggregate data is stored. To drop the index, specify the schema. For
example, to drop the index avg_temp_idx, run:
DROP INDEX _timescaledb_internal.avg_temp_idxLimitations on created indexes
Section titled “Limitations on created indexes”In TimescaleDB v2.7 and later, you can create an index on any column in the materialized view. This includes aggregated columns, such as those storing sums and averages. In earlier versions of TimescaleDB, you can’t create an index on an aggregated column.
You can’t create unique indexes on a continuous aggregate, in any of the TimescaleDB versions.