You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2022/10/18 11:54:21 UTC

[iotdb] branch IOTDB-4619 updated: Add doc for cq

This is an automated email from the ASF dual-hosted git repository.

jackietien pushed a commit to branch IOTDB-4619
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/IOTDB-4619 by this push:
     new a408558ed1 Add doc for cq
a408558ed1 is described below

commit a408558ed17801d1f0c73b14db26904bcc3bc61f
Author: JackieTien97 <ja...@gmail.com>
AuthorDate: Tue Oct 18 19:54:11 2022 +0800

    Add doc for cq
---
 docs/UserGuide/Process-Data/Continuous-Query.md    | 668 ++++++++++++++------
 docs/zh/UserGuide/Process-Data/Continuous-Query.md | 673 +++++++++++++++------
 2 files changed, 965 insertions(+), 376 deletions(-)

diff --git a/docs/UserGuide/Process-Data/Continuous-Query.md b/docs/UserGuide/Process-Data/Continuous-Query.md
index 8b8690c65c..3d0fe550c9 100644
--- a/docs/UserGuide/Process-Data/Continuous-Query.md
+++ b/docs/UserGuide/Process-Data/Continuous-Query.md
@@ -19,77 +19,85 @@
 
 -->
 
-# Continuous Query, CQ
+# Continuous Query(CQ)
 
-We can create, drop a CQ, and query all registered CQ configuration information through SQL statements.
+## Introduction
+Continuous queries(CQ) are queries that run automatically and periodically on realtime data and store query results in other specified time series.
 
-Note that the current distributed version of IoTDB does not support continuous queries. Please stay tuned.
-
-## SQL statements
-
-### Create CQ
-
-#### Syntax
+## Syntax
 
 ```sql
 CREATE (CONTINUOUS QUERY | CQ) <cq_id> 
-[RESAMPLE EVERY <every_interval> FOR <for_interval>]
+[RESAMPLE 
+  [EVERY <every_interval>] 
+  [BOUNDARY <execution_boundary_time>]
+  [RANGE <start_time_offset>[, end_time_offset]] 
+]
+[TIMEOUT POLICY BLOCKED|DISCARD]
 BEGIN
-SELECT <function>(<path_suffix>) INTO <full_path> | <node_name>
-FROM <path_prefix>
-GROUP BY time(<group_by_interval>) [, level = <level>]
+  SELECT CLAUSE
+    INTO CLAUSE
+    FROM CLAUSE
+    [WHERE CLAUSE]
+    [GROUP BY(<group_by_interval>[, <sliding_step>]) [, level = <level>]]
+    [HAVING CLAUSE]
+    [FILL {PREVIOUS | LINEAR | constant}]
+    [LIMIT rowLimit OFFSET rowOffset]
+    [ALIGN BY DEVICE]
 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.
 
-* `<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. 
-* `<for_interval>` specifies the time range of each query as `[now()-<for_interval>, now())`. We currently support the units of ns, us, ms, s, m, h, d, w.
-* `<execution_boundary_time>` is a date that represents **the start time of the first window**.
-  * `<execution_boundary_time>` can be earlier than, equals to, later than **current time**.
-  * This parameter is optional. If not specified, it is equivalent to `BOUNDARY now()`.
-  * **The end time of the first window** is `<execution_boundary_time> + <for_interval>`.
-  * The **start time** of the `i (1 <= i)`th window is `<execution_boundary_time> + (i - 1) * <every_interval>`.
-  * The **end time** of the `i (1 <= i)`th window is`<execution_boundary_time> + <for_interval> + (i - 1) * <every_interval>`.
-  * If **current time** is earlier than or equal to **the end time of the first window**, then the first execution moment of the continuous query is **the end time of the first window**.
-  * If **current time** is later than **the end time of the first window**, then the first execution moment of the continuous query is the **end time of the first window whose end time is later than or equal to the current time** .
-  * The **query time range**  at each execution moment is `[now() - <for_interval>, now())`.
-
-* `<function>` specifies the aggregate function.
-* `<path_prefix>` and `<path_suffix>` are spliced into the queried time series path.
-* `<full_path>` or `<node_name>` specifies the result time series path.
-* `<group_by_interval>` specifies the time grouping length. We currently support the units of ns, us, ms, s, m, h, d, w, mo, y.
-* `<level>` refers to grouping according to the `<level>` level of the time series, and grouping the aggregation result of  time series with the same name below the `<level>` level. For the specific semantics of the Group By Level statement and the definition of `<level>`, see [aggregation-by-level](../Write-And-Delete-Data/Delete-Data.md)
-
-
-Note:
-* `<for_interval>`,`<every_interval>` can optionally be specified. If the user does not specify one of them, the value of the unspecified item will be processed equal to `<group_by_interval>`.
-    * The values of `<every_interval>`, `<for_interval>` and `<group_by_interval>` should all be greater than 0.
-    * The value of `<group_by_interval>` should be less than the value of `<for_interval>`, otherwise the system will process the value equal to `<for_interval>`.
-    * The user should specify the appropriate `<for_interval>` and `<every_interval>` according to actual needs.
-        * If `<for_interval>` is greater than `<every_interval>`, there will be partial data overlap in each query window. This configuration is not recommended from the perspective of query performance.
-        * If `<for_interval>` is less than `<every_interval>`, there may be uncovered data between each query window.
-* For the result series path
-    * The user can choose to specify `<full_path>`, which is the complete time series path starting with `root`. The user can use the `${x}` variable in the path to represent the node name of `level = x` in the original time series. `x` should be greater than or equal to 0 and less than or equal to the value of `<level>`
-      (If `level` is not specified, it should be less than or equal to the level, i.e. length, of `<path_prefix>`).
-    * The user can also specify only `<node_name>`, which is the last node name of the result time series path.
-        * If the user specifies `<level> = l`, the result time series path generated by the system is `root.${1}. ... .${l}.<node_name>`
-        * If the user does not specify `<level>`, let the maximum level of the original time series be `L`,
-          Then the result time series path generated by the system is `root.${1}. ... .${L-1}.<node_name>`.
+### Descriptions of parameters in CQ syntax
 
-#### Examples
+- `<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.It'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.It'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 .
 
-##### Original Data
-````
-+-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                   timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln.wf02.wt02.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf02.wt01.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt02.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt01.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-+-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-````
+> - `<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](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic1.png?raw=true)
+
+#### `<start_time_offset>` > `<every_interval>`
+
+![2](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic2.png?raw=true)
+
+#### `<start_time_offset>` < `<every_interval>`
+
+![3](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic3.png?raw=true)
 
+#### `<every_interval>`  is not zero
+
+![4](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic4.png?raw=true)
+
+
+- `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.
 ````
 +-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
 |                         Time|root.ln.wf02.wt02.temperature|root.ln.wf02.wt01.temperature|root.ln.wf01.wt02.temperature|root.ln.wf01.wt01.temperature|
@@ -106,182 +114,468 @@ Note:
 +-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
 ````
 
+### Configuring execution intervals
 
-##### Result time series path configuration example
+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`.
 
-For the above original time series, if the user specifies that the query aggregation level is `2`, the aggregation function is `avg`,
-The user can specify only the last node name of the generated time series in the `INTO` clause. If the user specifies it as `temperature_avg`, the full path generated by the system will be `root.${1}.${2}.temperature_avg` .
-The user can also specify the full path in the `INTO` clause, and the user can specify it as `root.${1}.${2}.temperature_avg`, `root.ln_cq.${2}.temperature_avg`, `root.${1}_cq.${2}.temperature_avg`, `root.${1}.${2}_cq.temperature_avg` etc.,
-It can also be specified as `root.${2}.${1}.temperature_avg` and others as needed.
-It should be noted that the `x` in `${x}` should be greater than or equal to `1` and less than or equal to the value of `<level>`
-(If `<level>` is not specified, it should be less than or equal to the length of `<path_prefix>`). In the above example, `x` should be less than or equal to `2`.
-
-##### Create `cq1`
-````sql
-CREATE CONTINUOUS QUERY cq1 
+```sql
+CREATE CONTINUOUS QUERY cq1
+RESAMPLE EVERY 20s
 BEGIN 
   SELECT max_value(temperature) 
-  INTO temperature_max 
+  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
   FROM root.ln.*.* 
   GROUP BY time(10s) 
 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:
 
-Query the maximum value of `root.ln.*.*.temperature` in the previous 10s every 10s (the results are grouped by 10s),
- and the results will be written to `root.${1}.${2}.${3}.temperature_max`,
-As a result, 4 new time series will be generated.
 ````
-+---------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                       timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+---------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln.wf02.wt02.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf02.wt01.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt02.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt01.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-+---------------------------------+-----+-------------+--------+--------+-----------+----+----------+
+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)`.
+`cq1` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq1` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
+
+`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:
 ````
+> SELECT temperature_max from root.ln.*.*;
 +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 |                         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|
 +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
-|2021-05-11T22:18:16.964+08:00|                            122.0|                             68.0|                             68.0|                            103.0|
-|2021-05-11T22:18:26.964+08:00|                            182.0|                            113.0|                             59.0|                            181.0|
-|2021-05-11T22:18:36.964+08:00|                             78.0|                             38.0|                            123.0|                             52.0|
-|2021-05-11T22:18:46.964+08:00|                            137.0|                            172.0|                            183.0|                            193.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
 +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 ````
-##### Create `cq2`
-````sql
-CREATE CONTINUOUS QUERY cq2 
-RESAMPLE EVERY 20s FOR 20s 
-BEGIN 
-  SELECT avg(temperature) 
-  INTO temperature_avg 
-  FROM root.ln.*.* 
-  GROUP BY time(10s), level=2 
+
+### 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.
+
+```sql
+CREATE CONTINUOUS QUERY cq2
+RESAMPLE RANGE 40s
+BEGIN
+  SELECT max_value(temperature)
+  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
+  FROM root.ln.*.*
+  GROUP BY time(10s)
 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:
+
 ````
-Query the average value of `root.ln.*.*.temperature` in the previous 20s every 20s (the results are grouped by 10s),
- and the results will be written to `root.${1}.${2}.temperature_avg`,
-As a result, 2 new time series will be generated.
-Among them, `root.ln.wf02.temperature_avg` is generated by the aggregation calculation of `root.ln.wf02.wt02.temperature` and `root.ln.wf02.wt01.temperature`,
-and `root.ln.wf01.temperature_avg` is generated by the aggregation calculation of `root.ln.wf01.wt02.temperature` and `root.ln.wf01.wt01.temperature`.
-````
-+----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                  timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln.wf02.temperature_avg| null|      root.ln|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.temperature_avg| null|      root.ln|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-+----------------------------+-----+-------------+--------+--------+-----------+----+----------+
+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)`.
+`cq2` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                             NULL|                             NULL|                             NULL|                             NULL|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq2` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq2` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
+
+`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:
+
 ````
-+-----------------------------+----------------------------+----------------------------+
-|                         Time|root.ln.wf02.temperature_avg|root.ln.wf01.temperature_avg|
-+-----------------------------+----------------------------+----------------------------+
-|2021-05-11T22:18:16.969+08:00|                       58.75|                        49.0|
-|2021-05-11T22:18:26.969+08:00|                        89.0|                      112.25|
-|2021-05-11T22:18:36.969+08:00|                       42.25|                        61.5|
-|2021-05-11T22:18:46.969+08:00|                      112.25|                      132.25|
-+-----------------------------+----------------------------+----------------------------+
+> SELECT temperature_max from root.ln.*.*;
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 ````
-##### Create `cq3`
-````sql
-CREATE CONTINUOUS QUERY cq3 
-RESAMPLE EVERY 20s FOR 20s 
-BEGIN 
-  SELECT avg(temperature) 
-  INTO root.ln_cq.${2}.temperature_avg 
-  FROM root.ln.*.* 
-  GROUP BY time(10s), level=2 
+
+### 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.
+
+```sql
+CREATE CONTINUOUS QUERY cq3
+RESAMPLE EVERY 20s RANGE 40s
+BEGIN
+  SELECT max_value(temperature)
+  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
+  FROM root.ln.*.*
+  GROUP BY time(10s)
+  FILL(100.0)
 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:
 
-The query mode is the same as `cq2`,
-and the results will be written to `root.ln_cq.${2}.temperature_avg`.
-As a result, 2 new time series will be generated.
-Among them, `root.ln_cq.wf02.temperature_avg` is generated by the aggregation calculation of `root.ln.wf02.wt02.temperature` and `root.ln.wf02.wt01.temperature`,
-and `root.ln_cq.wf01.temperature_avg` is generated by the aggregation calculation of `root.ln.wf01.wt02.temperature` and `root.ln.wf01.wt01.temperature`.
-    
 ````
-+-------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                     timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+-------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln_cq.wf02.temperature_avg| null|   root.ln_cq|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-|root.ln_cq.wf01.temperature_avg| null|   root.ln_cq|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-+-------------------------------+-----+-------------+--------+--------+-----------+----+----------+
+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)`.
+`cq3` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq3` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
+
+Notice that `cq3` will calculate the results for some time interval many times, so here are the results:
 ````
-+-----------------------------+-------------------------------+-------------------------------+
-|                         Time|root.ln_cq.wf02.temperature_avg|root.ln_cq.wf01.temperature_avg|
-+-----------------------------+-------------------------------+-------------------------------+
-|2021-05-11T22:18:16.971+08:00|                          58.75|                           49.0|
-|2021-05-11T22:18:26.971+08:00|                           89.0|                         112.25|
-|2021-05-11T22:18:36.971+08:00|                          42.25|                           61.5|
-|2021-05-11T22:18:46.971+08:00|                         112.25|                         132.25|
-+-----------------------------+-------------------------------+-------------------------------+
+> SELECT temperature_max from root.ln.*.*;
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 ````
 
-##### 创建 `cq4`
+### 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.
 
-````sql
-CREATE CONTINUOUS QUERY cq4 
-RESAMPLE EVERY 20s FOR 20s BOUNDARY 2022-01-14T23:00:00.000+08:00 
+```sql
+CREATE CONTINUOUS QUERY cq4
+RESAMPLE EVERY 20s RANGE 40s, 20s
 BEGIN 
-  SELECT avg(temperature) 
-  INTO root.ln_cq.${2}.temperature_avg 
-  FROM root.ln.*.* GROUP BY time(10s), level=2 
+  SELECT max_value(temperature) 
+  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
+  FROM root.ln.*.* 
+  GROUP BY time(10s) 
+  FILL(100.0)
 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:
+
+````
+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)`.
+`cq4` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq4` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
 
-This example is almost identical to creating cq3. The difference is that in this example the user specified `BOUNDARY 2022-01-14T23:00:00.000+08:00 `.
+Notice that `cq4` will calculate the results for all time intervals only once after a delay of 20 seconds, so here are the results:
+
+````
+> SELECT temperature_max from root.ln.*.*;
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+````
 
-Note that the first execution time of this CQ is later than the time in the example, so `2022-01-14T23:00:20.000+08:00` is the first execution time. Recursively, `2022-01-14T23:00:40.000+08:00` is the second execution moment, `2022-01-14T23:01:00.000+08:00` is the third execution moment...
+### CQ without group by clause
 
-The SQL statement executed at the first execution moment is `select avg(temperature) from root.ln.*.* group by ([2022-01-14T23:00:00.000+08:00, 2022-01-14T23:00: 20.000+08:00), 10s), level = 2`.
+Use an `EVERY` interval in the `RESAMPLE` clause to specify the CQ’s execution interval and the length of the CQ’s time range.
 
-The SQL statement executed at the second execution moment is `select avg(temperature) from root.ln.*.* group by ([2022-01-14T23:00:20.000+08:00, 2022-01-14T23:00: 40.000+08:00), 10s), level = 2`.
+```sql
+CREATE CONTINUOUS QUERY cq5
+RESAMPLE EVERY 20s
+BEGIN 
+  SELECT temperature + 1
+  INTO  root.precalculated_sg.::(temperature)
+  FROM root.ln.*.* 
+  align by device
+END
+```
 
-The SQL statement executed at the third execution moment is `select avg(temperature) from root.ln.*.* group by ([2022-01-14T23:00:40.000+08:00, 2022-01-14T23:01: 00.000+08:00), 10s), level = 2`.
+`cq5` calculates the `temperature + 1` under the `root.ln` prefix path and stores the results in the `root.precalculated_sg` storage group. 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()`.
 
-### Show CQ Information
+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:
 
-#### Syntax
-````sql
-SHOW (CONTINUOUS QUERIES | CQS) 
+````
+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)`.
+`cq5` generate 16 lines:
+>
++-----------------------------+-------------------------------+-----------+
+|                         Time|                         Device|temperature|
++-----------------------------+-------------------------------+-----------+
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt02|      123.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt02|       48.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt02|      183.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt02|       45.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt01|       46.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt01|       15.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt01|      114.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt01|       12.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt02|       12.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt02|       60.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt02|       30.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt02|       53.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt01|       15.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt01|      182.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt01|      181.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt01|       20.0|
++-----------------------------+-------------------------------+-----------+
+>
+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)`.
+`cq5` generate 12 lines:
+>
++-----------------------------+-------------------------------+-----------+
+|                         Time|                         Device|temperature|
++-----------------------------+-------------------------------+-----------+
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt02|       79.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt02|      138.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt02|       17.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt01|       39.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt01|      173.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt01|      125.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt02|      124.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt02|      136.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt02|      184.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt01|       53.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt01|      194.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt01|       19.0|
++-----------------------------+-------------------------------+-----------+
+>
 ````
 
-#### Example Result
+`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:
+
 ````
-+-------+--------------+------------+-------------+----------------------------------------------------------------------------------------+-----------------------------------+
-|cq name|every interval|for interval|     boundary|                                                                               query sql|                        target path|
-+-------+--------------+------------+-------------+----------------------------------------------------------------------------------------+-----------------------------------+
-|    cq1|         10000|       10000|1642166102238|     select max_value(temperature) from root.ln.*.* group by ([now() - 10s, now()), 10s)|root.${1}.${2}.${3}.temperature_max|
-|    cq3|         20000|       20000|1642166118339|select avg(temperature) from root.ln.*.* group by ([now() - 20s, now()), 10s), level = 2|    root.ln_cq.${2}.temperature_avg|
-|    cq2|         20000|       20000|1642166111493|select avg(temperature) from root.ln.*.* group by ([now() - 20s, now()), 10s), level = 2|     root.${1}.${2}.temperature_avg|
-|    cq4|         20000|       20000|1642172400000|select avg(temperature) from root.ln.*.* group by ([now() - 20s, now()), 10s), level = 2|    root.ln_cq.${2}.temperature_avg|
-+-------+--------------+------------+-------------+----------------------------------------------------------------------------------------+-----------------------------------+
+> SELECT temperature from root.precalculated_sg.*.* align by device;
++-----------------------------+-------------------------------+-----------+
+|                         Time|                         Device|temperature|
++-----------------------------+-------------------------------+-----------+
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt02|      123.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt02|       48.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt02|      183.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt02|       45.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt02|       79.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt02|      138.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt02|       17.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt01|       46.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt01|       15.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt01|      114.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt01|       12.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt01|       39.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt01|      173.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt01|      125.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt02|       12.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt02|       60.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt02|       30.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt02|       53.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt02|      124.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt02|      136.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt02|      184.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt01|       15.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt01|      182.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt01|      181.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt01|       20.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt01|       53.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt01|      194.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt01|       19.0|
++-----------------------------+-------------------------------+-----------+
 ````
 
-### Drop CQ
-#### Syntax
-````sql
-DROP (CONTINUOUS QUERY | CQ) <cq_id> 
-````
+## CQ Management
+
+### Listing continuous queries
+
+List every CQ on the IoTDB Cluster with:
+
+```sql
+SHOW (CONTINUOUS QUERIES | CQS) 
+```
+
+`SHOW (CONTINUOUS QUERIES | CQS)` order results by `cq_id`.
+
+#### Examples
+
+```sql
+SHOW CONTINUOUS QUERIES;
+```
+
+we will get:
+
+| cq_id        | query                                                                                                                                 | state |
+|:-------------|---------------------------------------------------------------------------------------------------------------------------------------|-------|
+| s1_count_cq  | CREATE CQ s1_count_cq<br/>BEGIN<br/>SELECT count(s1)<br/>INTO root.sg_count.d.count_s1<br/>FROM root.sg.d<br/>GROUP BY(30m)<br/>END   | active |
+
+
+### Dropping continuous queries
+
+Drop a CQ with a specific `cq_id`:
+
+```sql
+DROP (CONTINUOUS QUERY | CQ) <cq_id>
+```
+
+DROP CQ returns an empty result.
+
+#### Examples
+
+Drop the CQ named `s1_count_cq`:
+
+```sql
+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 re`CREATE` it with the updated settings.
+
+
+## CQ Use Cases
+
+### Downsampling and Data Retention
+
+Use CQs with `TTL` set on storage group 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
 
-````sql
-DROP CONTINUOUS QUERY cq3
-````
+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:
+
+```sql
+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.
+
+```sql
+CREATE CQ s1_count_cq 
+BEGIN 
+    SELECT count(s1)  
+        INTO root.sg_count.d.count_s1
+        FROM root.sg.d
+        GROUP BY(30m)
+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:
+
+```sql
+SELECT avg(count_s1) from root.sg_count.d;
+```
+
 
-````sql
-DROP CQ cq3
-````
 ## System Parameter Configuration
 | Name | Description | Data Type | Default Value |
-| :---------------------------------- |-------- | ---- | -----|
-| `continuous_query_execution_thread` | The number of threads in the thread pool that executes continuous query tasks | int | max(1, CPU core number / 2)|
-| `max_pending_continuous_query_tasks` | The maximum number of continuous query tasks pending in queue | int | 64|
-| `continuous_query_min_every_interval` | The minimum value of the continuous query execution time interval | duration | 1s|
\ No newline at end of file
+| :---------------------------------- |-------- |-----------|---------------|
+| `continuous_query_submit_thread` | The number of threads in the scheduled thread pool that submit continuous query tasks periodically | int32     | 2             |
+| `continuous_query_min_every_interval_in_ms` | The minimum value of the continuous query execution time interval | duration  | 1000          |
\ No newline at end of file
diff --git a/docs/zh/UserGuide/Process-Data/Continuous-Query.md b/docs/zh/UserGuide/Process-Data/Continuous-Query.md
index 20e706b953..0d22a04576 100644
--- a/docs/zh/UserGuide/Process-Data/Continuous-Query.md
+++ b/docs/zh/UserGuide/Process-Data/Continuous-Query.md
@@ -21,76 +21,83 @@
 
 # 连续查询(Continuous Query, CQ)
 
-我们可以通过 SQL 语句注册、或卸载一个 CQ 实例,以及查询到所有已经注册的 CQ 配置信息。
+## 简介
+连续查询(Continuous queries, aka CQ) 是对实时数据周期性地自动执行的查询,并将查询结果写入指定的时间序列中。
 
-注意,目前连续查询尚未对分布式场景进行适配。敬请期待分布式版本。
-
-## SQL 语句
-
-### 创建 CQ
-
-#### 语法
+## 语法
 
 ```sql
 CREATE (CONTINUOUS QUERY | CQ) <cq_id> 
-[RESAMPLE EVERY <every_interval> FOR <for_interval> BOUNDARY <execution_boundary_time>] 
+[RESAMPLE 
+  [EVERY <every_interval>] 
+  [BOUNDARY <execution_boundary_time>]
+  [RANGE <start_time_offset>[, end_time_offset]] 
+]
+[TIMEOUT POLICY BLOCKED|DISCARD]
 BEGIN 
-SELECT <function>(<path_suffix>) INTO <full_path> | <node_name>
-FROM <path_prefix>
-GROUP BY time(<group_by_interval>) [, level = <level>] 
+    SELECT CLAUSE
+        INTO CLAUSE
+        FROM CLAUSE
+        [WHERE CLAUSE]
+        [GROUP BY(<group_by_interval>[, <sliding_step>]) [, level = <level>]]
+        [HAVING CLAUSE]
+        [FILL {PREVIOUS | LINEAR | constant}]
+        [LIMIT rowLimit OFFSET rowOffset]
+        [ALIGN BY DEVICE]
 END
 ```
 
-其中:
-
-* `<cq_id>` 指定 CQ 全局唯一的 id。
-* `<every_interval>` 指定查询执行时间间隔,支持 ns、us、ms、s、m、h、d、w 等单位,其值不应小于用户所配置的 `continuous_query_min_every_interval` 值。可选择指定。
-* `<for_interval>` 指定每次查询的窗口大小,即查询时间范围为`[now() - <for_interval>, now())`,其中 `now()` 指查询时的时间戳。支持 ns、us、ms、s、m、h、d、w 等单位。可选择指定。 
-* `<execution_boundary_time>` 是一个日期参数,表示**第一个窗口的起始时间**。
-  * `<execution_boundary_time>` 可早于、等于、晚于**当前时间**。
-  * 该参数可选择指定,不指定的情况下等价于输入 `BOUNDARY now()`。
-  * **第一个窗口的结束时间**为  `<execution_boundary_time> + <for_interval>`。
-  * 第 ` i (1 <= i)` 个窗口的**开始时间** `<execution_boundary_time> + (i - 1) * <every_interval>`。
-  * 第 ` i (1 <= i)` 个窗口的**结束时间** `<execution_boundary_time> + <for_interval> + (i - 1) * <every_interval>`。
-  * 如果**当前时间**小于等于**第一个窗口的结束时间** ,那么连续查询的第一个执行时刻为**第一个窗口的结束时间**。
-  * 如果**当前时间**大于**第一个窗口的结束时间**,那么连续查询的第一个执行时刻为**第一个**大于等于**当前时间**的**窗口结束时间**。
-  * 每一个执行时刻执行的**查询时间范围**为`[now() - <for_interval>, now())`。
-
-* `<function>` 指定聚合函数,目前支持 `count`, `sum`, `avg`, `last_value`, `first_value`, `min_time`, `max_time`, `min_value`, `max_value` 等。
-* `<path_prefix>` 与 `<path_suffix>` 拼接成完整的查询原时间序列。
-* `<full_path>` 或 `<node_name>` 指定将查询出的数据写入的结果序列路径。
-* `<group_by_interval>` 指定时间分组长度,支持 ns、us、ms、s、m、h、d、w、mo、y 等单位。
-* `<level>`指按照序列第 `<level>` 层分组,将第 `<level>` 层同名的所有序列聚合。Group By Level 语句的具体语义及 `<level>` 的定义见 [路径层级分组聚合](../Operate-Metadata/DML-Data-Manipulation-Language.md)。
-
-注:
-
-* `<for_interval>`, `<every_interval>` 可选择指定。如果用户没有指定其中的某一项,则未指定项的值按照`<group_by_interval>` 处理。
-    * `<every_interval>`,`<for_interval>`,`<group_by_interval>` 的值均应大于 0。
-    * `<group_by_interval>` 的值应小于`<for_interval>`的值,否则系统会按照等于`<for_interval>`的值处理。 
-    * 用户应当结合实际需求指定合适的 `<for_interval>` 与 `<every_interval>`。
-      * 若 `<for_interval>` 大于 `<every_interval>`,每次的查询窗口会有部分数据重叠,从查询性能角度这种配置不被建议。
-      * 若 `<for_interval>` 小于 `<every_interval>`,每次的查询窗口之间可能会有未覆盖到的数据。
-*  对于结果序列路径
-     * 用户可以选择指定`<full_path>`,即以 `root` 开头的完整的时间序列路径,用户可以在路径中使用 `${x}` 变量来表示原始时间序列中 `level = x` 的节点名称,`x`应当大于等于 1 且小于等于 `<level>` 值
-       (若未指定 `level`,则应小于等于 `<path_prefix>` 长度)。
-    * 用户也可以仅指定`<node_name>`,即生成时间序列路径的最后一个结点名。
-      * 若用户指定  `<level> = l`,则系统生成的结果时间序列路径为 `root.${1}. ... .${l}.<node_name>`
-      * 若用户未指定 `<level>`,令原始时间序列最大层数为 `L`, 
-      则系统生成的结果时间序列路径为 `root.${1}. ... .${L - 1}.<node_name>`。
-
-#### 示例
-
-##### 原始时间序列
-````
-+-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                   timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln.wf02.wt02.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf02.wt01.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt02.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt01.temperature| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-+-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-````
+> 注意:
+> 1. 如果where子句中出现任何时间过滤条件,IoTDB将会抛出异常,因为IoTDB会自动为每次查询执行指定时间范围。
+> 2. GROUP BY TIME CLAUSE在连续查询中的语法稍有不同,它不能包含原来的第一个参数,即 [start_time, end_time),IoTDB会自动填充这个缺失的参数。如果指定,IoTDB将会抛出异常。
+> 3. 如果连续查询中既没有GROUP BY TIME子句,也没有指定EVERY子句,IoTDB将会抛出异常。
+
+### 连续查询语法中参数含义的描述
+
+- `<cq_id>` 为连续查询指定一个全局唯一的标识。
+- `<every_interval>` 指定了连续查询周期性执行的间隔。现在支持的时间单位有:ns, us, ms, s, m, h, d, w, 并且它的值不能小于用户在`iotdb-confignode.properties`配置文件中指定的`continuous_query_min_every_interval`。这是一个可选参数,默认等于group by子句中的`group_by_interval`。
+- `<start_time_offset>` 指定了每次查询执行窗口的开始时间,即`now()-<start_time_offset>`。现在支持的时间单位有:ns, us, ms, s, m, h, d, w。这是一个可选参数,默认等于`EVERY`子句中的`every_interval`。
+- `<end_time_offset>` 指定了每次查询执行窗口的结束时间,即`now()-<end_time_offset>`。现在支持的时间单位有:ns, us, ms, s, m, h, d, w。这是一个可选参数,默认等于`0`.
+- `<execution_boundary_time>` 表示用户期待的连续查询的首个周期任务的执行时间。(因为连续查询只会对当前实时的数据流做计算,所以该连续查询实际首个周期任务的执行时间并不一定等于用户指定的时间,具体计算逻辑如下所示)
+    - `<execution_boundary_time>` 可以早于、等于或者迟于当前时间。
+    - 这个参数是可选的,默认等于`0`。
+    - 首次查询执行窗口的开始时间为`<execution_boundary_time> - <start_time_offset>`.
+    - 首次查询执行窗口的结束时间为`<execution_boundary_time> - <end_time_offset>`.
+    - 第i个查询执行窗口的时间范围是`[<execution_boundary_time> - <start_time_offset> + (i - 1) * <every_interval>, <execution_boundary_time> - <end_time_offset> + (i - 1) * <every_interval>)`。
+    - 如果当前时间早于或等于, 那连续查询的首个周期任务的执行时间就是用户指定的`execution_boundary_time`.
+    - 如果当前时间迟于用户指定的`execution_boundary_time`,那么连续查询的首个周期任务的执行时间就是`execution_boundary_time + i * <every_interval>`中第一个大于或等于当前时间的值。
+
+> - <every_interval>,<start_time_offset> 和 <group_by_interval> 都应该大于 0
+> - <group_by_interval>应该小于等于<start_time_offset>
+> - 用户应该根据实际需求,为<start_time_offset> 和 <every_interval> 指定合适的值
+>     - 如果<start_time_offset>大于<every_interval>,在每一次查询执行的时间窗口上会有部分重叠
+>     - 如果<start_time_offset>小于<every_interval>,在连续的两次查询执行的时间窗口中间将会有未覆盖的时间范围
+> - start_time_offset 应该大于end_time_offset
+
+#### `<start_time_offset>`等于`<every_interval>`
+
+![1](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic1.png?raw=true)
+
+#### `<start_time_offset>`大于`<every_interval>`
+
+![2](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic2.png?raw=true)
+
+#### `<start_time_offset>`小于`<every_interval>`
+
+![3](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic3.png?raw=true)
+
+#### `<every_interval>`不为0
+
+![4](https://github.com/apache/iotdb-bin-resources/blob/main/docs/UserGuide/Process-Data/Continuous-Query/pic4.png?raw=true)
+
+- `TIMEOUT POLICY` 指定了我们如何处理“前一个时间窗口还未执行完时,下一个窗口的执行时间已经到达的场景,默认值是`BLOCKED`.
+    - `BLOCKED`意味着即使下一个窗口的执行时间已经到达,我们依旧需要阻塞等待前一个时间窗口的查询执行完再开始执行下一个窗口。如果使用`BLOCKED`策略,所有的时间窗口都将会被依此执行,但是如果遇到执行查询的时间长于周期性间隔时,连续查询的结果会迟于最新的时间窗口范围。
+    - `DISCARD`意味着如果前一个时间窗口还未执行完,我们会直接丢弃下一个窗口的执行时间。如果使用`DISCARD`策略,可能会有部分时间窗口得不到执行。但是一旦前一个查询执行完后,它将会使用最新的时间窗口,所以它的执行结果总能赶上最新的时间窗口范围,当然是以部分时间窗口得不到执行为代价。
+
+
+## 连续查询的用例
+
+下面是用例数据,这是一个实时的数据流,我们假设数据都按时到达。
 
 ````
 +-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
@@ -108,182 +115,470 @@ END
 +-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
 ````
 
-##### 结果序列配置举例说明
+### 配置连续查询执行的周期性间隔
 
-对于以上原始时间序列,若用户指定查询聚合层级为 `2`,聚合函数为 `avg`,
-用户可以在 `INTO` 语句中仅指定生成序列的最后一个结点名,若用户将其指定为 `temperature_avg`,则系统生成的完整路径为 `root.${1}.${2}.temperature_avg`。
-用户也可以在 `INTO` 语句中指定完整写入路径,用户可将其指定为 `root.${1}.${2}.temperature_avg`、`root.ln_cq.${2}.temperature_avg`、`root.${1}_cq.${2}.temperature_avg`、`root.${1}.${2}_cq.temperature_avg`等,
-也可以按需要指定为 `root.${2}.${1}.temperature_avg` 等其它形式。
-需要注意的是,`${x}` 中的 `x` 应当大于等于 `1` 且小于等于 `<level>` 值
-(若未指定 `<level>`,则应小于等于 `<path_prefix>` 层级)。在上例中,`x` 应当小于等于 `2`。
+在`RESAMPLE`子句中使用`EVERY`参数指定连续查询的执行间隔,如果没有指定,默认等于`group_by_interval`。
 
-##### 创建 `cq1`
-````sql
-CREATE CONTINUOUS QUERY cq1 
+```sql
+CREATE CONTINUOUS QUERY cq1
+RESAMPLE EVERY 20s
 BEGIN 
   SELECT max_value(temperature) 
-  INTO temperature_max 
+  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
   FROM root.ln.*.* 
   GROUP BY time(10s) 
 END
-````
+```
+
+`cq1`计算出`temperature`传感器每10秒的平均值,并且将查询结果存储在`temperature_max`传感器下,传感器路径前缀使用跟原来一样的前缀。
+
+`cq1`每20秒执行一次,每次执行的查询的时间窗口范围是从过去20秒到当前时间。
 
-每隔 10s 查询 `root.ln.*.*.temperature` 在前 10s 内的最大值(结果以 10s 为一组),
-将结果写入到 `root.${1}.${2}.${3}.temperature_max` 中,
-结果将产生 4 条新序列:
+假设当前时间是`2021-05-11T22:18:40.000+08:00`,如果把日志等级设置为DEBUG,我们可以在`cq1`执行的DataNode上看到如下的输出:
 
 ````
-+---------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                       timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+---------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln.wf02.wt02.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf02.wt01.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt02.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.wt01.temperature_max| null|      root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|
-+---------------------------------+-----+-------------+--------+--------+-----------+----+----------+
+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)`.
+`cq1` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq1` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
+
+`cq1`并不会处理当前时间窗口以外的数据,即`2021-05-11T22:18:20.000+08:00`以前的数据,所以我们会得到如下结果:
+
 ````
+> SELECT temperature_max from root.ln.*.*;
 +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 |                         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|
 +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
-|2021-05-11T22:18:16.964+08:00|                            122.0|                             68.0|                             68.0|                            103.0|
-|2021-05-11T22:18:26.964+08:00|                            182.0|                            113.0|                             59.0|                            181.0|
-|2021-05-11T22:18:36.964+08:00|                             78.0|                             38.0|                            123.0|                             52.0|
-|2021-05-11T22:18:46.964+08:00|                            137.0|                            172.0|                            183.0|                            193.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
 +-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 ````
-##### 创建 `cq2`
-````sql
-CREATE CONTINUOUS QUERY cq2 
-RESAMPLE EVERY 20s FOR 20s 
-BEGIN 
-  SELECT avg(temperature) 
-  INTO temperature_avg 
-  FROM root.ln.*.* 
-  GROUP BY time(10s), level=2 
+
+### 配置连续查询的时间窗口大小
+
+使用`RANGE`子句中的`start_time_offset`参数指定连续查询每次执行的时间窗口的开始时间偏移,如果没有指定,默认值等于`EVERY`参数。
+
+```sql
+CREATE CONTINUOUS QUERY cq2
+RESAMPLE RANGE 40s
+BEGIN
+  SELECT max_value(temperature)
+  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
+  FROM root.ln.*.*
+  GROUP BY time(10s)
 END
-````
+```
 
-每隔 20s 查询 `root.ln.*.*.temperature` 在前 20s 内的平均值(结果以 10s 为一组,按照第 2 层节点分组),
-将结果写入到 `root.${1}.${2}.temperature_avg` 中。
-结果将产生如下两条新序列,
-其中 `root.ln.wf02.temperature_avg` 由 `root.ln.wf02.wt02.temperature` 和 `root.ln.wf02.wt01.temperature` 聚合计算生成,
-`root.ln.wf01.temperature_avg` 由 `root.ln.wf01.wt02.temperature` 和 `root.ln.wf01.wt01.temperature` 聚合计算生成。
+`cq2`计算出`temperature`传感器每10秒的平均值,并且将查询结果存储在`temperature_max`传感器下,传感器路径前缀使用跟原来一样的前缀。
+
+`cq2`每10秒执行一次,每次执行的查询的时间窗口范围是从过去40秒到当前时间。
+
+假设当前时间是`2021-05-11T22:18:40.000+08:00`,如果把日志等级设置为DEBUG,我们可以在`cq2`执行的DataNode上看到如下的输出:
 
 ````
-+----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                  timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+----------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln.wf02.temperature_avg| null|      root.ln|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-|root.ln.wf01.temperature_avg| null|      root.ln|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-+----------------------------+-----+-------------+--------+--------+-----------+----+----------+
+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)`.
+`cq2` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                             NULL|                             NULL|                             NULL|                             NULL|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq2` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq2` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
+
+`cq2`并不会写入全是null值的行,值得注意的是`cq2`会多次计算某些区间的聚合值,下面是计算结果:
+
 ````
-+-----------------------------+----------------------------+----------------------------+
-|                         Time|root.ln.wf02.temperature_avg|root.ln.wf01.temperature_avg|
-+-----------------------------+----------------------------+----------------------------+
-|2021-05-11T22:18:16.969+08:00|                       58.75|                        49.0|
-|2021-05-11T22:18:26.969+08:00|                        89.0|                      112.25|
-|2021-05-11T22:18:36.969+08:00|                       42.25|                        61.5|
-|2021-05-11T22:18:46.969+08:00|                      112.25|                      132.25|
-+-----------------------------+----------------------------+----------------------------+
+> SELECT temperature_max from root.ln.*.*;
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 ````
-##### 创建 `cq3`
-````sql
-CREATE CONTINUOUS QUERY cq3 
-RESAMPLE EVERY 20s FOR 20s 
-BEGIN 
-  SELECT avg(temperature) 
-  INTO root.ln_cq.${2}.temperature_avg 
-  FROM root.ln.*.* 
-  GROUP BY time(10s), level=2 
+
+### 同时配置连续查询执行的周期性间隔和时间窗口大小
+
+使用`RESAMPLE`子句中的`EVERY`参数和`RANGE`参数分别指定连续查询的执行间隔和窗口大小。并且使用`fill()`来填充没有值的时间区间。
+
+```sql
+CREATE CONTINUOUS QUERY cq3
+RESAMPLE EVERY 20s RANGE 40s
+BEGIN
+  SELECT max_value(temperature)
+  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
+  FROM root.ln.*.*
+  GROUP BY time(10s)
+  FILL(100.0)
 END
-````
-查询模式与 cq2 相同,在这个例子中,用户自行指定结果写入到 `root.ln_cq.${2}.temperature_avg` 中。
-结果将产生如下两条新序列,
-其中 `root.ln_cq.wf02.temperature_avg` 由 `root.ln.wf02.wt02.temperature` 和 `root.ln.wf02.wt01.temperature` 聚合计算生成,
-`root.ln_cq.wf01.temperature_avg` 由 `root.ln.wf01.wt02.temperature` 和 `root.ln.wf01.wt01.temperature` 聚合计算生成。
+```
+
+`cq3`计算出`temperature`传感器每10秒的平均值,并且将查询结果存储在`temperature_max`传感器下,传感器路径前缀使用跟原来一样的前缀。如果某些区间没有值,用`100.0`填充。
+
+`cq3`每20秒执行一次,每次执行的查询的时间窗口范围是从过去40秒到当前时间。
+
+假设当前时间是`2021-05-11T22:18:40.000+08:00`,如果把日志等级设置为DEBUG,我们可以在`cq3`执行的DataNode上看到如下的输出:
 
 ````
-+-------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|                     timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
-+-------------------------------+-----+-------------+--------+--------+-----------+----+----------+
-|root.ln_cq.wf02.temperature_avg| null|   root.ln_cq|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-|root.ln_cq.wf01.temperature_avg| null|   root.ln_cq|  DOUBLE| GORILLA|     SNAPPY|null|      null|
-+-------------------------------+-----+-------------+--------+--------+-----------+----+----------+
+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)`.
+`cq3` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq3` generate 4 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
+
+值得注意的是`cq3`会多次计算某些区间的聚合值,下面是计算结果:
+
 ````
-+-----------------------------+-------------------------------+-------------------------------+
-|                         Time|root.ln_cq.wf02.temperature_avg|root.ln_cq.wf01.temperature_avg|
-+-----------------------------+-------------------------------+-------------------------------+
-|2021-05-11T22:18:16.971+08:00|                          58.75|                           49.0|
-|2021-05-11T22:18:26.971+08:00|                           89.0|                         112.25|
-|2021-05-11T22:18:36.971+08:00|                          42.25|                           61.5|
-|2021-05-11T22:18:46.971+08:00|                         112.25|                         132.25|
-+-----------------------------+-------------------------------+-------------------------------+
+> SELECT temperature_max from root.ln.*.*;
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
+|2021-05-11T22:18:40.000+08:00|                            137.0|                            172.0|                            135.0|                            193.0|
+|2021-05-11T22:18:50.000+08:00|                             16.0|                            124.0|                            183.0|                             18.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
 ````
 
-##### 创建 `cq4`
+### 配置连续查询每次查询执行时间窗口的结束时间
 
-````sql
-CREATE CONTINUOUS QUERY cq4 
-RESAMPLE EVERY 20s FOR 20s BOUNDARY 2022-01-14T23:00:00.000+08:00 
-BEGIN 
-  SELECT avg(temperature) 
-  INTO root.ln_cq.${2}.temperature_avg 
-  FROM root.ln.*.* GROUP BY time(10s), level=2 
+使用`RESAMPLE`子句中的`EVERY`参数和`RANGE`参数分别指定连续查询的执行间隔和窗口大小。并且使用`fill()`来填充没有值的时间区间。
+
+```sql
+CREATE CONTINUOUS QUERY cq4
+RESAMPLE EVERY 20s RANGE 40s, 20s
+BEGIN
+  SELECT max_value(temperature)
+  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
+  FROM root.ln.*.*
+  GROUP BY time(10s)
+  FILL(100.0)
 END
+```
+
+`cq4`计算出`temperature`传感器每10秒的平均值,并且将查询结果存储在`temperature_max`传感器下,传感器路径前缀使用跟原来一样的前缀。如果某些区间没有值,用`100.0`填充。
+
+`cq4`每20秒执行一次,每次执行的查询的时间窗口范围是从过去40秒到过去20秒。
+
+假设当前时间是`2021-05-11T22:18:40.000+08:00`,如果把日志等级设置为DEBUG,我们可以在`cq4`执行的DataNode上看到如下的输出:
+
+````
+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)`.
+`cq4` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
+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)`.
+`cq4` generate 2 lines:
+>
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+>
 ````
 
-这个例子与创建 cq3 几乎完全相同。不同的是,在这个例子中用户自行指定了 `BOUNDARY 2022-01-14T23:00:00.000+08:00 ` 。
+值得注意的是`cq4`只会计算每个聚合区间一次,并且每次开始执行计算的时间都会比当前的时间窗口结束时间迟20s, 下面是计算结果:
 
-注意这个 CQ 的第一个执行时刻大于例子中的时间,因此 `2022-01-14T23:00:20.000+08:00` 为第一个执行时刻。递推地,`2022-01-14T23:00:40.000+08:00` 为第二个执行时刻,`2022-01-14T23:01:00.000+08:00` 为第三个执行时刻…… 
+````
+> SELECT temperature_max from root.ln.*.*;
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|                         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|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+|2021-05-11T22:18:00.000+08:00|                            100.0|                            100.0|                            100.0|                            100.0|
+|2021-05-11T22:18:10.000+08:00|                            121.0|                             72.0|                            183.0|                            115.0|
+|2021-05-11T22:18:20.000+08:00|                            122.0|                             45.0|                             59.0|                            181.0|
+|2021-05-11T22:18:30.000+08:00|                            182.0|                            113.0|                             52.0|                            180.0|
++-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
+````
 
-第一个执行时刻执行的 SQL 语句为 `select avg(temperature) from root.ln.*.* group by ([2022-01-14T23:00:00.000+08:00, 2022-01-14T23:00:20.000+08:00), 10s), level = 2`。
+### 没有GROUP BY TIME子句的连续查询
 
-第二个执行时刻执行的 SQL 语句为 `select avg(temperature) from root.ln.*.* group by ([2022-01-14T23:00:20.000+08:00, 2022-01-14T23:00:40.000+08:00), 10s), level = 2`。
+不使用`GROUP BY TIME`子句,并在`RESAMPLE`子句中显式使用`EVERY`参数指定连续查询的执行间隔。
 
-第三个执行时刻执行的 SQL 语句为 `select avg(temperature) from root.ln.*.* group by ([2022-01-14T23:00:40.000+08:00, 2022-01-14T23:01:00.000+08:00), 10s), level = 2`。
+```sql
+CREATE CONTINUOUS QUERY cq5
+RESAMPLE EVERY 20s
+BEGIN
+  SELECT temperature + 1
+  INTO  root.precalculated_sg.::(temperature)
+  FROM root.ln.*.*
+  align by device
+END
+```
 
-……
+`cq5`计算以`root.ln`为前缀的所有`temperature + 1`的值,并将结果储存在另一个存储组`root.precalculated_sg`中。除存储组名称不同外,目标序列与源序列路径名均相同。
 
+`cq5`每20秒执行一次,每次执行的查询的时间窗口范围是从过去20秒到当前时间。
 
-### 展示 CQ 信息
+假设当前时间是`2021-05-11T22:18:40.000+08:00`,如果把日志等级设置为DEBUG,我们可以在`cq5`执行的DataNode上看到如下的输出:
 
-#### 语法
-````sql
-SHOW (CONTINUOUS QUERIES | CQS) 
-````
-#### 结果示例
 ````
-+-------+--------------+------------+-------------+----------------------------------------------------------------------------------------+-----------------------------------+
-|cq name|every interval|for interval|     boundary|                                                                               query sql|                        target path|
-+-------+--------------+------------+-------------+----------------------------------------------------------------------------------------+-----------------------------------+
-|    cq1|         10000|       10000|1642166102238|     select max_value(temperature) from root.ln.*.* group by ([now() - 10s, now()), 10s)|root.${1}.${2}.${3}.temperature_max|
-|    cq3|         20000|       20000|1642166118339|select avg(temperature) from root.ln.*.* group by ([now() - 20s, now()), 10s), level = 2|    root.ln_cq.${2}.temperature_avg|
-|    cq2|         20000|       20000|1642166111493|select avg(temperature) from root.ln.*.* group by ([now() - 20s, now()), 10s), level = 2|     root.${1}.${2}.temperature_avg|
-|    cq4|         20000|       20000|1642172400000|select avg(temperature) from root.ln.*.* group by ([now() - 20s, now()), 10s), level = 2|    root.ln_cq.${2}.temperature_avg|
-+-------+--------------+------------+-------------+----------------------------------------------------------------------------------------+-----------------------------------+
+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)`.
+`cq5` generate 16 lines:
+>
++-----------------------------+-------------------------------+-----------+
+|                         Time|                         Device|temperature|
++-----------------------------+-------------------------------+-----------+
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt02|      123.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt02|       48.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt02|      183.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt02|       45.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt01|       46.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt01|       15.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt01|      114.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt01|       12.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt02|       12.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt02|       60.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt02|       30.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt02|       53.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt01|       15.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt01|      182.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt01|      181.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt01|       20.0|
++-----------------------------+-------------------------------+-----------+
+>
+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)`.
+`cq5` generate 12 lines:
+>
++-----------------------------+-------------------------------+-----------+
+|                         Time|                         Device|temperature|
++-----------------------------+-------------------------------+-----------+
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt02|       79.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt02|      138.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt02|       17.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt01|       39.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt01|      173.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt01|      125.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt02|      124.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt02|      136.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt02|      184.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt01|       53.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt01|      194.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt01|       19.0|
++-----------------------------+-------------------------------+-----------+
+>
 ````
-### 删除 CQ
-#### 语法
-````sql
-DROP (CONTINUOUS QUERY | CQ) <cq_id> 
+
+`cq5`并不会处理当前时间窗口以外的数据,即`2021-05-11T22:18:20.000+08:00`以前的数据,所以我们会得到如下结果:
+
 ````
-#### 示例
-````sql
-DROP CONTINUOUS QUERY cq3
+> SELECT temperature from root.precalculated_sg.*.* align by device;
++-----------------------------+-------------------------------+-----------+
+|                         Time|                         Device|temperature|
++-----------------------------+-------------------------------+-----------+
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt02|      123.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt02|       48.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt02|      183.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt02|       45.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt02|       79.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt02|      138.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt02|       17.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf02.wt01|       46.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf02.wt01|       15.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf02.wt01|      114.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf02.wt01|       12.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf02.wt01|       39.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf02.wt01|      173.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf02.wt01|      125.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt02|       12.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt02|       60.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt02|       30.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt02|       53.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt02|      124.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt02|      136.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt02|      184.0|
+|2021-05-11T22:18:24.949+08:00|root.precalculated_sg.wf01.wt01|       15.0| 
+|2021-05-11T22:18:29.967+08:00|root.precalculated_sg.wf01.wt01|      182.0|
+|2021-05-11T22:18:34.979+08:00|root.precalculated_sg.wf01.wt01|      181.0|
+|2021-05-11T22:18:39.990+08:00|root.precalculated_sg.wf01.wt01|       20.0|
+|2021-05-11T22:18:44.995+08:00|root.precalculated_sg.wf01.wt01|       53.0| 
+|2021-05-11T22:18:49.999+08:00|root.precalculated_sg.wf01.wt01|      194.0|
+|2021-05-11T22:18:55.003+08:00|root.precalculated_sg.wf01.wt01|       19.0|
++-----------------------------+-------------------------------+-----------+
 ````
 
-``` sql
-DROP CQ cq3
+## 连续查询的管理
+
+### 查询系统已有的连续查询
+
+展示集群中所有的已注册的连续查询
+
+```sql
+SHOW (CONTINUOUS QUERIES | CQS) 
+```
+
+`SHOW (CONTINUOUS QUERIES | CQS)`会将结果集按照`cq_id`排序。
+
+#### 例子
+
+```sql
+SHOW CONTINUOUS QUERIES;
 ```
 
-## 系统参数配置
+执行以上sql,我们将会得到如下的查询结果:
+
+| cq_id        | query                                                                                                                                 | state |
+|:-------------|---------------------------------------------------------------------------------------------------------------------------------------|-------|
+| s1_count_cq  | CREATE CQ s1_count_cq<br/>BEGIN<br/>SELECT count(s1)<br/>INTO root.sg_count.d.count_s1<br/>FROM root.sg.d<br/>GROUP BY(30m)<br/>END   | active |
+
+
+### 删除已有的连续查询
+
+删除指定的名为cq_id的连续查询:
+
+```sql
+DROP (CONTINUOUS QUERY | CQ) <cq_id>
+```
+
+DROP CQ并不会返回任何结果集。
+
+#### 例子
+
+删除名为s1_count_cq的连续查询:
+
+```sql
+DROP CONTINUOUS QUERY s1_count_cq;
+```
+
+### 修改已有的连续查询
+
+目前连续查询一旦被创建就不能再被修改。如果想要修改某个连续查询,只能先用`DROP`命令删除它,然后再用`CREATE`命令重新创建。
+
+
+## 连续查询的使用场景
+
+### 对数据进行降采样并对降采样后的数据使用不同的保留策略
+
+可以使用连续查询,定期将高频率采样的原始数据(如每秒1000个点),降采样(如每秒仅保留一个点)后保存到另一个存储组的同名序列中。高精度的原始数据所在存储组的`TTL`可能设置的比较短,比如一天,而低精度的降采样后的数据所在的存储组`TTL`可以设置的比较长,比如一个月,从而达到快速释放磁盘空间的目的。
+
+### 预计算代价昂贵的查询
+
+我们可以通过连续查询对一些重复的查询进行预计算,并将查询结果保存在某些目标序列中,这样真实查询并不需要真的再次去做计算,而是直接查询目标序列的结果,从而缩短了查询的时间。
+
+> 预计算查询结果尤其对一些可视化工具渲染时序图和工作台时有很大的加速作用。
+
+### 作为子查询的替代品
+
+IoTDB现在不支持子查询,但是我们可以通过创建连续查询得到相似的功能。我们可以将子查询注册为一个连续查询,并将子查询的结果物化到目标序列中,外层查询再直接查询哪个目标序列。
+
+#### 例子
+
+IoTDB并不会接收如下的嵌套子查询。这个查询会计算s1序列每隔30分钟的非空值数量的平均值:
+
+```sql
+SELECT avg(count_s1) from (select count(s1) as count_s1 from root.sg.d group by([0, now()), 30m));
+```
+
+为了得到相同的结果,我们可以:
+
+**1. 创建一个连续查询**
+
+这一步执行内层子查询部分。下面创建的连续查询每隔30分钟计算一次`root.sg.d.s1`序列的非空值数量,并将结果写入目标序列`root.sg_count.d.count_s1`中。
+
+```sql
+CREATE CQ s1_count_cq 
+BEGIN 
+    SELECT count(s1)  
+        INTO root.sg_count.d.count_s1
+        FROM root.sg.d
+        GROUP BY(30m)
+END
+```
+
+**2. 查询连续查询的结果**
+
+这一步执行外层查询的avg([...])部分。
+
+查询序列`root.sg_count.d.count_s1`的值,并计算平均值:
+
+```sql
+SELECT avg(count_s1) from root.sg_count.d;
+```
 
-| 参数名          | 描述           |  数据类型| 默认值 |
-| :---------------------------------- |-------- | ----| -----|
-| `continuous_query_execution_thread` | 执行连续查询任务的线程池的线程数 | int | max(1, CPU 核数 / 2)|
-| `max_pending_continuous_query_tasks` | 队列中连续查询最大任务堆积数 | int | 64|
-| `continuous_query_min_every_interval` | 连续查询执行时间间隔的最小值 | duration | 1s|
 
+## 连续查询相关的配置参数
+| 参数名 | 描述                   | 类型 | 默认值 |
+| :---------------------------------- |----------------------|----------|---------------|
+| `continuous_query_submit_thread` | 用于周期性提交连续查询执行任务的线程数  | int32    | 2             |
+| `continuous_query_min_every_interval_in_ms` | 系统允许的连续查询最小的周期性时间间隔  | duration | 1000          |
\ No newline at end of file