CONTINUOUS QUERY
Manage LindormTSDB continuous queries to auto-aggregate time series data, list existing queries, and delete them.
Applicable engines and versions
Applies to LindormTSDB only. Supported by all versions.
CREATE CONTINUOUS QUERY
A continuous query (CQ) runs automatically on time series data in LindormTSDB at a fixed interval, aggregating recent data and writing the results to a destination table. Use CQs to downsample high-frequency data for long-term storage without manual intervention.
Syntax
create_cq_statement ::= CREATE CONTINUOUS QUERY [database_identifier.] cq_identifier
WITH ( cq_attribute_statement )
AS insert_select_statement
cq_attribute_statement ::= attribute_definition (',' attribute_definition)*
attribute_definition ::= attr_identifier '=' attr_valHow it works
Each CQ runs on a fixed schedule controlled by three attributes:
`interval` — how often the CQ runs (required)
`window` — the time range of source data each run covers (defaults to
interval)`offset` — shifts the execution time relative to the UTC epoch (defaults to no shift)
At each scheduled execution, LindormTSDB determines the time range automatically based on these attributes and writes the aggregated results to the destination table. The write statement does not require an explicit time filter.
Parameters
database_identifier
The name of the database that owns the CQ. If omitted, the current database is used. Enclose the name in backticks. Example: ` db_sensor `.
cq_identifier
The name of the CQ. Enclose the name in backticks. Example: ` my_cq `.
cq_attribute_statement
Attributes that control how the CQ runs:
Attribute | Type | Required | Default | Description |
| STRING | Yes | — | How often the CQ runs. The interval value can be accurate to seconds. Format: |
| STRING | No | Same as | The time range of source data each run covers. The window is inclusive of the start time and exclusive of the end time. Example: |
| STRING | No | No offset (aligned to | Shifts the execution time relative to the UTC epoch. Use this to align runs with a specific local time. Example: |
insert_select_statement
The write statement that defines the aggregation logic and destination. Key constraints:
Do not include a time filter in the
SELECTclause. LindormTSDB derives the time range from the CQ attributes automatically.The number of columns in the
SELECTclause must match the destination table, including all fields and tags.For the full syntax, see Write data.
Example
The following CQ runs every hour and aggregates the past two hours of sensor data into hourly averages.
CREATE CONTINUOUS QUERY `default`.`my_cq`
WITH (`INTERVAL`='1h', `WINDOW`='2h')
AS
INSERT INTO `default`.`sensor`
SELECT AVG(`temperature`) AS `temperature`,
AVG(`humidity`) AS `humidity`,
`device_id`,
`region`
FROM `default`.`sensor`
SAMPLE BY 1h;What this CQ does at each run:
Calculates the average
temperatureandhumidity, grouped bydevice_idandregion.Writes the results back to the
sensortable in thedefaultdatabase.
SHOW CONTINUOUS QUERIES
Lists all continuous queries in the current database. To view continuous queries in a different database, use the USE statement to switch databases first.
Syntax
show_cq_statement ::= SHOW CONTINUOUS QUERIESReturned results
Column name | Type | Description |
name | VARCHAR | The name of the continuous query. |
interval | VARCHAR | The interval at which the continuous query runs. |
window | VARCHAR | The time window of the data. Only data updated within the latest time window is included in the continuous query's calculations. |
offset | VARCHAR | The offset of the data time window. |
query | VARCHAR | The statement executed in the continuous query. |
Examples
List all continuous queries in the current database.
SHOW CONTINUOUS QUERIES;The following result is returned:
+-------+----------+--------------------------------+
| name | interval | query |
+-------+----------+--------------------------------+
| my_cq | 1d | INSERT INTO |
| | | `db_sensor_year`.`sensor` |
| | | (SELECT MEAN(`temperature`) |
| | | AS `temperature`, |
| | | MEAN(`humidity`) AS |
| | | `humidity`, `device_id`, |
| | | `region` FROM |
| | | `db_sensor_month`.`sensor`) |
+-------+----------+--------------------------------+DROP CONTINUOUS QUERY
Deletes a continuous query from a LindormTSDB database.
Syntax
DROP CONTINUOUS QUERY [database_identifier.cq_identifier]Parameters
Parameter | Description |
| The name of the database that contains the continuous query. If omitted, defaults to the current database. Enclose the name in backticks. Example: ` |
| The name of the continuous query to delete. Enclose the name in backticks. Example: ` |
Examples
Delete the continuous query my_cq from the db_sensor database:
DROP CONTINUOUS QUERY `db_sensor`.`my_cq`;