Continuous Query(CQ)

Introduction

Continuous queries(CQ) are queries that run automatically and periodically on realtime data and store query results in other specified time series.

Syntax

  1. CREATE (CONTINUOUS QUERY | CQ) <cq_id>
  2. [RESAMPLE
  3. [EVERY <every_interval>]
  4. [BOUNDARY <execution_boundary_time>]
  5. [RANGE <start_time_offset>[, end_time_offset]]
  6. ]
  7. [TIMEOUT POLICY BLOCKED|DISCARD]
  8. BEGIN
  9. SELECT CLAUSE
  10. INTO CLAUSE
  11. FROM CLAUSE
  12. [WHERE CLAUSE]
  13. [GROUP BY(<group_by_interval>[, <sliding_step>]) [, level = <level>]]
  14. [HAVING CLAUSE]
  15. [FILL {PREVIOUS | LINEAR | constant}]
  16. [LIMIT rowLimit OFFSET rowOffset]
  17. [ALIGN BY DEVICE]
  18. END

Note:

  1. If there exists any time filters in WHERE CLAUSE, IoTDB will throw an error, because IoTDB will automatically generate a time range for the query each time it’s executed.
  2. GROUP BY TIME CLAUSE is different, it doesn’t contain its original first display window parameter which is [start_time, end_time). It’s still because IoTDB will automatically generate a time range for the query each time it’s executed.
  3. If there is no group by time clause in query, EVERY clause is required, otherwise IoTDB will throw an error.

Descriptions of parameters in CQ syntax

  • <cq_id> specifies the globally unique id of CQ.
  • <every_interval> specifies the query execution time interval. We currently support the units of ns, us, ms, s, m, h, d, w, and its value should not be lower than the minimum threshold configured by the user, which is continuous_query_min_every_interval. It’s an optional parameter, default value is set to group_by_interval in group by clause.
  • <start_time_offset> specifies the start time of each query execution as now()-<start_time_offset>. We currently support the units of ns, us, ms, s, m, h, d, w.ItContinuous Query - 图1open in new window‘s an optional parameter, default value is set to every_interval in resample clause.
  • <end_time_offset> specifies the end time of each query execution as now()-<end_time_offset>. We currently support the units of ns, us, ms, s, m, h, d, w.ItContinuous Query - 图2open in new window‘s an optional parameter, default value is set to 0.
  • <execution_boundary_time> is a date that represents the execution time of a certain cq task.
    • <execution_boundary_time> can be earlier than, equals to, later than current time.
    • This parameter is optional. If not specified, it is equal to BOUNDARY 0
    • The start time of the first time window is <execution_boundary_time> - <start_time_offset>.
    • The end time of the first time window is <execution_boundary_time> - <end_time_offset>.
    • The time range of the i (1 <= i)th window is [<execution_boundary_time> - <start_time_offset> + (i - 1) * <every_interval>, <execution_boundary_time> - <end_time_offset> + (i - 1) * <every_interval>).
    • If the current time is earlier than or equal to execution_boundary_time, then the first execution moment of the continuous query is execution_boundary_time.
    • If the current time is later than execution_boundary_time, then the first execution moment of the continuous query is the first execution_boundary_time + i * <every_interval> that is later than or equal to the current time .
  • <every_interval><start_time_offset> and <group_by_interval> should all be greater than 0.
  • The value of <group_by_interval> should be less than or equal to the value of <start_time_offset>, otherwise the system will throw an error.
  • Users should specify the appropriate <start_time_offset> and <every_interval> according to actual needs.
    • If <start_time_offset> is greater than <every_interval>, there will be partial data overlap in each query window.
    • If <start_time_offset> is less than <every_interval>, there may be uncovered data between each query window.
  • start_time_offset should be larger than end_time_offset, otherwise the system will throw an error.

<start_time_offset> == <every_interval>

1

1

<start_time_offset> > <every_interval>

2

2

<start_time_offset> < <every_interval>

3

3

<every_interval> is not zero

4

4

  • TIMEOUT POLICY specify how we deal with the cq task whose previous time interval execution is not finished while the next execution time has reached. The default value is BLOCKED.
    • BLOCKED means that we will block and wait to do the current cq execution task until the previous time interval cq task finishes. If using BLOCKED policy, all the time intervals will be executed, but it may be behind the latest time interval.
    • DISCARD means that we just discard the current cq execution task and wait for the next execution time and do the next time interval cq task. If using DISCARD policy, some time intervals won’t be executed when the execution time of one cq task is longer than the <every_interval>. However, once a cq task is executed, it will use the latest time interval, so it can catch up at the sacrifice of some time intervals being discarded.

Examples of CQ

The examples below use the following sample data. It’s a real time data stream and we can assume that the data arrives on time.

  1. +-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
  2. | Time|root.ln.wf02.wt02.temperature|root.ln.wf02.wt01.temperature|root.ln.wf01.wt02.temperature|root.ln.wf01.wt01.temperature|
  3. +-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
  4. |2021-05-11T22:18:14.598+08:00| 121.0| 72.0| 183.0| 115.0|
  5. |2021-05-11T22:18:19.941+08:00| 0.0| 68.0| 68.0| 103.0|
  6. |2021-05-11T22:18:24.949+08:00| 122.0| 45.0| 11.0| 14.0|
  7. |2021-05-11T22:18:29.967+08:00| 47.0| 14.0| 59.0| 181.0|
  8. |2021-05-11T22:18:34.979+08:00| 182.0| 113.0| 29.0| 180.0|
  9. |2021-05-11T22:18:39.990+08:00| 42.0| 11.0| 52.0| 19.0|
  10. |2021-05-11T22:18:44.995+08:00| 78.0| 38.0| 123.0| 52.0|
  11. |2021-05-11T22:18:49.999+08:00| 137.0| 172.0| 135.0| 193.0|
  12. |2021-05-11T22:18:55.003+08:00| 16.0| 124.0| 183.0| 18.0|
  13. +-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+

Configuring execution intervals

Use an EVERY interval in the RESAMPLE clause to specify the CQ’s execution interval, if not specific, default value is equal to group_by_interval.

  1. CREATE CONTINUOUS QUERY cq1
  2. RESAMPLE EVERY 20s
  3. BEGIN
  4. SELECT max_value(temperature)
  5. INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max)
  6. FROM root.ln.*.*
  7. GROUP BY(10s)
  8. END

cq1 calculates the 10-second average of temperature sensor under the root.ln prefix path and stores the results in the temperature_max sensor using the same prefix path as the corresponding sensor.

cq1 executes at 20-second intervals, the same interval as the EVERY interval. Every 20 seconds, cq1 runs a single query that covers the time range for the current time bucket, that is, the 20-second time bucket that intersects with now().

Supposing that the current time is 2021-05-11T22:18:40.000+08:00, we can see annotated log output about cq1 running at DataNode if you set log level to DEBUG:

  1. At **2021-05-11T22:18:40.000+08:00**, `cq1` executes a query within the time range `[2021-05-11T22:18:20, 2021-05-11T22:18:40)`.
  2. `cq1` generate 2 lines:
  3. >
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  6. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  7. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  8. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  9. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  10. >
  11. At **2021-05-11T22:19:00.000+08:00**, `cq1` executes a query within the time range `[2021-05-11T22:18:40, 2021-05-11T22:19:00)`.
  12. `cq1` generate 2 lines:
  13. >
  14. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  15. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  16. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  17. |2021-05-11T22:18:40.000+08:00| 137.0| 172.0| 135.0| 193.0|
  18. |2021-05-11T22:18:50.000+08:00| 16.0| 124.0| 183.0| 18.0|
  19. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  20. >

cq1 won’t deal with data that is before the current time window which is 2021-05-11T22:18:20.000+08:00, so here are the results:

  1. > SELECT temperature_max from root.ln.*.*;
  2. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  3. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  6. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  7. |2021-05-11T22:18:40.000+08:00| 137.0| 172.0| 135.0| 193.0|
  8. |2021-05-11T22:18:50.000+08:00| 16.0| 124.0| 183.0| 18.0|
  9. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+

Configuring time range for resampling

Use start_time_offset in the RANGE clause to specify the start time of the CQ’s time range, if not specific, default value is equal to EVERY interval.

  1. CREATE CONTINUOUS QUERY cq2
  2. RESAMPLE RANGE 40s
  3. BEGIN
  4. SELECT max_value(temperature)
  5. INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max)
  6. FROM root.ln.*.*
  7. GROUP BY(10s)
  8. END

cq2 calculates the 10-second average of temperature sensor under the root.ln prefix path and stores the results in the temperature_max sensor using the same prefix path as the corresponding sensor.

cq2 executes at 10-second intervals, the same interval as the group_by_interval. Every 10 seconds, cq2 runs a single query that covers the time range between now() minus the start_time_offset and now() , that is, the time range between 40 seconds prior to now() and now().

Supposing that the current time is 2021-05-11T22:18:40.000+08:00, we can see annotated log output about cq2 running at DataNode if you set log level to DEBUG:

  1. At **2021-05-11T22:18:40.000+08:00**, `cq2` executes a query within the time range `[2021-05-11T22:18:00, 2021-05-11T22:18:40)`.
  2. `cq2` generate 4 lines:
  3. >
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  6. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  7. |2021-05-11T22:18:00.000+08:00| NULL| NULL| NULL| NULL|
  8. |2021-05-11T22:18:10.000+08:00| 121.0| 72.0| 183.0| 115.0|
  9. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  10. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  11. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  12. >
  13. At **2021-05-11T22:18:50.000+08:00**, `cq2` executes a query within the time range `[2021-05-11T22:18:10, 2021-05-11T22:18:50)`.
  14. `cq2` generate 4 lines:
  15. >
  16. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  17. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  18. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  19. |2021-05-11T22:18:10.000+08:00| 121.0| 72.0| 183.0| 115.0|
  20. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  21. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  22. |2021-05-11T22:18:40.000+08:00| 137.0| 172.0| 135.0| 193.0|
  23. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  24. >
  25. At **2021-05-11T22:19:00.000+08:00**, `cq2` executes a query within the time range `[2021-05-11T22:18:20, 2021-05-11T22:19:00)`.
  26. `cq2` generate 4 lines:
  27. >
  28. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  29. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  30. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  31. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  32. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  33. |2021-05-11T22:18:40.000+08:00| 137.0| 172.0| 135.0| 193.0|
  34. |2021-05-11T22:18:50.000+08:00| 16.0| 124.0| 183.0| 18.0|
  35. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  36. >

cq2 won’t write lines that are all null. Notice cq2 will also calculate the results for some time interval many times. Here are the results:

  1. > SELECT temperature_max from root.ln.*.*;
  2. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  3. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. |2021-05-11T22:18:10.000+08:00| 121.0| 72.0| 183.0| 115.0|
  6. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  7. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  8. |2021-05-11T22:18:40.000+08:00| 137.0| 172.0| 135.0| 193.0|
  9. |2021-05-11T22:18:50.000+08:00| 16.0| 124.0| 183.0| 18.0|
  10. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+

Configuring execution intervals and CQ time ranges

Use an EVERY interval and RANGE interval in the RESAMPLE clause to specify the CQ’s execution interval and the length of the CQ’s time range. And use fill() to change the value reported for time intervals with no data.

  1. CREATE CONTINUOUS QUERY cq3
  2. RESAMPLE EVERY 20s RANGE 40s
  3. BEGIN
  4. SELECT max_value(temperature)
  5. INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max)
  6. FROM root.ln.*.*
  7. GROUP BY(10s)
  8. FILL(100.0)
  9. END

cq3 calculates the 10-second average of temperature sensor under the root.ln prefix path and stores the results in the temperature_max sensor using the same prefix path as the corresponding sensor. Where possible, it writes the value 100.0 for time intervals with no results.

cq3 executes at 20-second intervals, the same interval as the EVERY interval. Every 20 seconds, cq3 runs a single query that covers the time range between now() minus the start_time_offset and now(), that is, the time range between 40 seconds prior to now() and now().

Supposing that the current time is 2021-05-11T22:18:40.000+08:00, we can see annotated log output about cq3 running at DataNode if you set log level to DEBUG:

  1. At **2021-05-11T22:18:40.000+08:00**, `cq3` executes a query within the time range `[2021-05-11T22:18:00, 2021-05-11T22:18:40)`.
  2. `cq3` generate 4 lines:
  3. >
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  6. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  7. |2021-05-11T22:18:00.000+08:00| 100.0| 100.0| 100.0| 100.0|
  8. |2021-05-11T22:18:10.000+08:00| 121.0| 72.0| 183.0| 115.0|
  9. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  10. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  11. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  12. >
  13. At **2021-05-11T22:19:00.000+08:00**, `cq3` executes a query within the time range `[2021-05-11T22:18:20, 2021-05-11T22:19:00)`.
  14. `cq3` generate 4 lines:
  15. >
  16. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  17. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  18. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  19. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  20. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  21. |2021-05-11T22:18:40.000+08:00| 137.0| 172.0| 135.0| 193.0|
  22. |2021-05-11T22:18:50.000+08:00| 16.0| 124.0| 183.0| 18.0|
  23. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  24. >

Notice that cq3 will calculate the results for some time interval many times, so here are the results:

  1. > SELECT temperature_max from root.ln.*.*;
  2. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  3. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. |2021-05-11T22:18:00.000+08:00| 100.0| 100.0| 100.0| 100.0|
  6. |2021-05-11T22:18:10.000+08:00| 121.0| 72.0| 183.0| 115.0|
  7. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  8. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  9. |2021-05-11T22:18:40.000+08:00| 137.0| 172.0| 135.0| 193.0|
  10. |2021-05-11T22:18:50.000+08:00| 16.0| 124.0| 183.0| 18.0|
  11. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+

Configuring end_time_offset for CQ time range

Use an EVERY interval and RANGE interval in the RESAMPLE clause to specify the CQ’s execution interval and the length of the CQ’s time range. And use fill() to change the value reported for time intervals with no data.

  1. CREATE CONTINUOUS QUERY cq4
  2. RESAMPLE EVERY 20s RANGE 40s, 20s
  3. BEGIN
  4. SELECT max_value(temperature)
  5. INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max)
  6. FROM root.ln.*.*
  7. GROUP BY(10s)
  8. FILL(100.0)
  9. END

cq4 calculates the 10-second average of temperature sensor under the root.ln prefix path and stores the results in the temperature_max sensor using the same prefix path as the corresponding sensor. Where possible, it writes the value 100.0 for time intervals with no results.

cq4 executes at 20-second intervals, the same interval as the EVERY interval. Every 20 seconds, cq4 runs a single query that covers the time range between now() minus the start_time_offset and now() minus the end_time_offset, that is, the time range between 40 seconds prior to now() and 20 seconds prior to now().

Supposing that the current time is 2021-05-11T22:18:40.000+08:00, we can see annotated log output about cq4 running at DataNode if you set log level to DEBUG:

  1. At **2021-05-11T22:18:40.000+08:00**, `cq4` executes a query within the time range `[2021-05-11T22:18:00, 2021-05-11T22:18:20)`.
  2. `cq4` generate 2 lines:
  3. >
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  6. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  7. |2021-05-11T22:18:00.000+08:00| 100.0| 100.0| 100.0| 100.0|
  8. |2021-05-11T22:18:10.000+08:00| 121.0| 72.0| 183.0| 115.0|
  9. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  10. >
  11. At **2021-05-11T22:19:00.000+08:00**, `cq4` executes a query within the time range `[2021-05-11T22:18:20, 2021-05-11T22:18:40)`.
  12. `cq4` generate 2 lines:
  13. >
  14. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  15. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  16. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  17. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  18. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  19. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  20. >

Notice that cq4 will calculate the results for all time intervals only once after a delay of 20 seconds, so here are the results:

  1. > SELECT temperature_max from root.ln.*.*;
  2. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  3. | Time|root.ln.wf02.wt02.temperature_max|root.ln.wf02.wt01.temperature_max|root.ln.wf01.wt02.temperature_max|root.ln.wf01.wt01.temperature_max|
  4. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
  5. |2021-05-11T22:18:00.000+08:00| 100.0| 100.0| 100.0| 100.0|
  6. |2021-05-11T22:18:10.000+08:00| 121.0| 72.0| 183.0| 115.0|
  7. |2021-05-11T22:18:20.000+08:00| 122.0| 45.0| 59.0| 181.0|
  8. |2021-05-11T22:18:30.000+08:00| 182.0| 113.0| 52.0| 180.0|
  9. +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+

CQ without group by clause

Use an EVERY interval in the RESAMPLE clause to specify the CQ’s execution interval and the length of the CQ’s time range.

  1. CREATE CONTINUOUS QUERY cq5
  2. RESAMPLE EVERY 20s
  3. BEGIN
  4. SELECT temperature + 1
  5. INTO root.precalculated_sg.::(temperature)
  6. FROM root.ln.*.*
  7. align by device
  8. END

cq5 calculates the temperature + 1 under the root.ln prefix path and stores the results in the root.precalculated_sg database. Sensors use the same prefix path as the corresponding sensor.

cq5 executes at 20-second intervals, the same interval as the EVERY interval. Every 20 seconds, cq5 runs a single query that covers the time range for the current time bucket, that is, the 20-second time bucket that intersects with now().

Supposing that the current time is 2021-05-11T22:18:40.000+08:00, we can see annotated log output about cq5 running at DataNode if you set log level to DEBUG:

  1. At **2021-05-11T22:18:40.000+08:00**, `cq5` executes a query within the time range `[2021-05-11T22:18:20, 2021-05-11T22:18:40)`.
  2. `cq5` generate 16 lines:
  3. >
  4. +-----------------------------+-------------------------------+-----------+
  5. | Time| Device|temperature|
  6. +-----------------------------+-------------------------------+-----------+
  7. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt02| 123.0|
  8. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt02| 48.0|
  9. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt02| 183.0|
  10. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt02| 45.0|
  11. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt01| 46.0|
  12. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt01| 15.0|
  13. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt01| 114.0|
  14. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt01| 12.0|
  15. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt02| 12.0|
  16. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt02| 60.0|
  17. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt02| 30.0|
  18. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt02| 53.0|
  19. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt01| 15.0|
  20. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt01| 182.0|
  21. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt01| 181.0|
  22. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt01| 20.0|
  23. +-----------------------------+-------------------------------+-----------+
  24. >
  25. At **2021-05-11T22:19:00.000+08:00**, `cq5` executes a query within the time range `[2021-05-11T22:18:40, 2021-05-11T22:19:00)`.
  26. `cq5` generate 12 lines:
  27. >
  28. +-----------------------------+-------------------------------+-----------+
  29. | Time| Device|temperature|
  30. +-----------------------------+-------------------------------+-----------+
  31. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt02| 79.0|
  32. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt02| 138.0|
  33. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt02| 17.0|
  34. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt01| 39.0|
  35. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt01| 173.0|
  36. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt01| 125.0|
  37. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt02| 124.0|
  38. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt02| 136.0|
  39. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt02| 184.0|
  40. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt01| 53.0|
  41. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt01| 194.0|
  42. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt01| 19.0|
  43. +-----------------------------+-------------------------------+-----------+
  44. >

cq5 won’t deal with data that is before the current time window which is 2021-05-11T22:18:20.000+08:00, so here are the results:

  1. > SELECT temperature from root.precalculated_sg.*.* align by device;
  2. +-----------------------------+-------------------------------+-----------+
  3. | Time| Device|temperature|
  4. +-----------------------------+-------------------------------+-----------+
  5. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt02| 123.0|
  6. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt02| 48.0|
  7. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt02| 183.0|
  8. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt02| 45.0|
  9. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt02| 79.0|
  10. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt02| 138.0|
  11. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt02| 17.0|
  12. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt01| 46.0|
  13. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt01| 15.0|
  14. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt01| 114.0|
  15. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt01| 12.0|
  16. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt01| 39.0|
  17. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt01| 173.0|
  18. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt01| 125.0|
  19. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt02| 12.0|
  20. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt02| 60.0|
  21. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt02| 30.0|
  22. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt02| 53.0|
  23. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt02| 124.0|
  24. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt02| 136.0|
  25. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt02| 184.0|
  26. |2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt01| 15.0|
  27. |2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt01| 182.0|
  28. |2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt01| 181.0|
  29. |2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt01| 20.0|
  30. |2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt01| 53.0|
  31. |2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt01| 194.0|
  32. |2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt01| 19.0|
  33. +-----------------------------+-------------------------------+-----------+

CQ Management

Listing continuous queries

List every CQ on the IoTDB Cluster with:

  1. SHOW (CONTINUOUS QUERIES | CQS)

SHOW (CONTINUOUS QUERIES | CQS) order results by cq_id.

Examples

  1. SHOW CONTINUOUS QUERIES;

we will get:

cq_idquerystate
s1_count_cqCREATE CQ s1_count_cq
BEGIN
SELECT count(s1)
INTO root.sg_count.d.count_s1
FROM root.sg.d
GROUP BY(30m)
END
active

Dropping continuous queries

Drop a CQ with a specific cq_id:

  1. DROP (CONTINUOUS QUERY | CQ) <cq_id>

DROP CQ returns an empty result.

Examples

Drop the CQ named s1_count_cq:

  1. DROP CONTINUOUS QUERY s1_count_cq;

Altering continuous queries

CQs can’t be altered once they’re created. To change a CQ, you must DROP and reCREATE it with the updated settings.

CQ Use Cases

Downsampling and Data Retention

Use CQs with TTL set on database in IoTDB to mitigate storage concerns. Combine CQs and TTL to automatically downsample high precision data to a lower precision and remove the dispensable, high precision data from the database.

Recalculating expensive queries

Shorten query runtimes by pre-calculating expensive queries with CQs. Use a CQ to automatically downsample commonly-queried, high precision data to a lower precision. Queries on lower precision data require fewer resources and return faster.

Pre-calculate queries for your preferred graphing tool to accelerate the population of graphs and dashboards.

Substituting for sub-query

IoTDB does not support sub queries. We can get the same functionality by creating a CQ as a sub query and store its result into other time series and then querying from those time series again will be like doing nested sub query.

Example

IoTDB does not accept the following query with a nested sub query. The query calculates the average number of non-null values of s1 at 30 minute intervals:

  1. SELECT avg(count_s1) from (select count(s1) as count_s1 from root.sg.d group by([0, now()), 30m));

To get the same results:

1. Create a CQ

This step performs the nested sub query in from clause of the query above. The following CQ automatically calculates the number of non-null values of s1 at 30 minute intervals and writes those counts into the new root.sg_count.d.count_s1 time series.

  1. CREATE CQ s1_count_cq
  2. BEGIN
  3. SELECT count(s1)
  4. INTO root.sg_count.d(count_s1)
  5. FROM root.sg.d
  6. GROUP BY(30m)
  7. END

2. Query the CQ results

Next step performs the avg([…]) part of the outer query above.

Query the data in the time series root.sg_count.d.count_s1 to calculate the average of it:

  1. SELECT avg(count_s1) from root.sg_count.d;

System Parameter Configuration

NameDescriptionData TypeDefault Value
continuous_query_submit_threadThe number of threads in the scheduled thread pool that submit continuous query tasks periodicallyint322
continuous_query_min_every_interval_in_msThe minimum value of the continuous query execution time intervalduration1000