blob: 4a77657e6c3c8062e64c3a02b60f224e0092067a [file] [log] [blame]
import{_ as i,C as o,O as l,P as r,ah as c,Q as e,U as n,ai as t,aW as s}from"./framework-e4340ccd.js";const d={},u=s(`<h1 id="continuous-query-cq" tabindex="-1"><a class="header-anchor" href="#continuous-query-cq" aria-hidden="true">#</a> Continuous Query(CQ)</h1><h2 id="introduction" tabindex="-1"><a class="header-anchor" href="#introduction" aria-hidden="true">#</a> Introduction</h2><p>Continuous queries(CQ) are queries that run automatically and periodically on realtime data and store query results in other specified time series.</p><h2 id="syntax" tabindex="-1"><a class="header-anchor" href="#syntax" aria-hidden="true">#</a> Syntax</h2><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token punctuation">(</span>CONTINUOUS QUERY <span class="token operator">|</span> CQ<span class="token punctuation">)</span> <span class="token operator">&lt;</span>cq_id<span class="token operator">&gt;</span>
<span class="token punctuation">[</span>RESAMPLE
<span class="token punctuation">[</span>EVERY <span class="token operator">&lt;</span>every_interval<span class="token operator">&gt;</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span>BOUNDARY <span class="token operator">&lt;</span>execution_boundary_time<span class="token operator">&gt;</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span>RANGE <span class="token operator">&lt;</span>start_time_offset<span class="token operator">&gt;</span><span class="token punctuation">[</span><span class="token punctuation">,</span> end_time_offset<span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span>
<span class="token punctuation">[</span>TIMEOUT POLICY BLOCKED<span class="token operator">|</span><span class="token keyword">DISCARD</span><span class="token punctuation">]</span>
<span class="token keyword">BEGIN</span>
<span class="token keyword">SELECT</span> CLAUSE
<span class="token keyword">INTO</span> CLAUSE
<span class="token keyword">FROM</span> CLAUSE
<span class="token punctuation">[</span><span class="token keyword">WHERE</span> CLAUSE<span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span><span class="token punctuation">(</span><span class="token operator">&lt;</span>group_by_interval<span class="token operator">&gt;</span><span class="token punctuation">[</span><span class="token punctuation">,</span> <span class="token operator">&lt;</span>sliding_step<span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">,</span> <span class="token keyword">level</span> <span class="token operator">=</span> <span class="token operator">&lt;</span><span class="token keyword">level</span><span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token keyword">HAVING</span> CLAUSE<span class="token punctuation">]</span>
<span class="token punctuation">[</span>FILL {PREVIOUS <span class="token operator">|</span> LINEAR <span class="token operator">|</span> constant}<span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token keyword">LIMIT</span> rowLimit <span class="token keyword">OFFSET</span> rowOffset<span class="token punctuation">]</span>
<span class="token punctuation">[</span>ALIGN <span class="token keyword">BY</span> DEVICE<span class="token punctuation">]</span>
<span class="token keyword">END</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote><p>Note:</p><ol><li>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&#39;s executed.</li><li>GROUP BY TIME CLAUSE is different, it doesn&#39;t contain its original first display window parameter which is [start_time, end_time). It&#39;s still because IoTDB will automatically generate a time range for the query each time it&#39;s executed.</li><li>If there is no group by time clause in query, EVERY clause is required, otherwise IoTDB will throw an error.</li></ol></blockquote><h3 id="descriptions-of-parameters-in-cq-syntax" tabindex="-1"><a class="header-anchor" href="#descriptions-of-parameters-in-cq-syntax" aria-hidden="true">#</a> Descriptions of parameters in CQ syntax</h3>`,7),p=e("li",null,[e("code",null,"<cq_id>"),n(" specifies the globally unique id of CQ.")],-1),m=e("li",null,[e("code",null,"<every_interval>"),n(" 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 "),e("code",null,"continuous_query_min_every_interval"),n(". It's an optional parameter, default value is set to "),e("code",null,"group_by_interval"),n(" in group by clause.")],-1),v=e("code",null,"<start_time_offset>",-1),h=e("code",null,"now()-<start_time_offset>",-1),b={href:"http://w.It",target:"_blank",rel:"noopener noreferrer"},g=e("code",null,"every_interval",-1),w=e("code",null,"<end_time_offset>",-1),k=e("code",null,"now()-<end_time_offset>",-1),f={href:"http://w.It",target:"_blank",rel:"noopener noreferrer"},_=e("code",null,"0",-1),x=s("<li><code>&lt;execution_boundary_time&gt;</code> is a date that represents the execution time of a certain cq task. <ul><li><code>&lt;execution_boundary_time&gt;</code> can be earlier than, equals to, later than <strong>current time</strong>.</li><li>This parameter is optional. If not specified, it is equal to <code>BOUNDARY 0</code>。</li><li><strong>The start time of the first time window</strong> is <code>&lt;execution_boundary_time&gt; - &lt;start_time_offset&gt;</code>.</li><li><strong>The end time of the first time window</strong> is <code>&lt;execution_boundary_time&gt; - &lt;end_time_offset&gt;</code>.</li><li>The <strong>time range</strong> of the <code>i (1 &lt;= i)th</code> window is <code>[&lt;execution_boundary_time&gt; - &lt;start_time_offset&gt; + (i - 1) * &lt;every_interval&gt;, &lt;execution_boundary_time&gt; - &lt;end_time_offset&gt; + (i - 1) * &lt;every_interval&gt;)</code>.</li><li>If the <strong>current time</strong> is earlier than or equal to <code>execution_boundary_time</code>, then the first execution moment of the continuous query is <code>execution_boundary_time</code>.</li><li>If the <strong>current time</strong> is later than <code>execution_boundary_time</code>, then the first execution moment of the continuous query is the first <code>execution_boundary_time + i * &lt;every_interval&gt;</code> that is later than or equal to the current time .</li></ul></li>",1),y=s(`<blockquote><ul><li><code>&lt;every_interval&gt;</code>,<code>&lt;start_time_offset&gt;</code> and <code>&lt;group_by_interval&gt;</code> should all be greater than <code>0</code>.</li><li>The value of <code>&lt;group_by_interval&gt;</code> should be less than or equal to the value of <code>&lt;start_time_offset&gt;</code>, otherwise the system will throw an error.</li><li>Users should specify the appropriate <code>&lt;start_time_offset&gt;</code> and <code>&lt;every_interval&gt;</code> according to actual needs. <ul><li>If <code>&lt;start_time_offset&gt;</code> is greater than <code>&lt;every_interval&gt;</code>, there will be partial data overlap in each query window.</li><li>If <code>&lt;start_time_offset&gt;</code> is less than <code>&lt;every_interval&gt;</code>, there may be uncovered data between each query window.</li></ul></li><li><code>start_time_offset</code> should be larger than <code>end_time_offset</code>, otherwise the system will throw an error.</li></ul></blockquote><h4 id="start-time-offset-every-interval" tabindex="-1"><a class="header-anchor" href="#start-time-offset-every-interval" aria-hidden="true">#</a> <code>&lt;start_time_offset&gt;</code> == <code>&lt;every_interval&gt;</code></h4><figure><img src="https://alioss.timecho.com/docs/img/UserGuide/Process-Data/Continuous-Query/pic1.png" alt="1" tabindex="0" loading="lazy"><figcaption>1</figcaption></figure><h4 id="start-time-offset-every-interval-1" tabindex="-1"><a class="header-anchor" href="#start-time-offset-every-interval-1" aria-hidden="true">#</a> <code>&lt;start_time_offset&gt;</code> &gt; <code>&lt;every_interval&gt;</code></h4><figure><img src="https://alioss.timecho.com/docs/img/UserGuide/Process-Data/Continuous-Query/pic2.png" alt="2" tabindex="0" loading="lazy"><figcaption>2</figcaption></figure><h4 id="start-time-offset-every-interval-2" tabindex="-1"><a class="header-anchor" href="#start-time-offset-every-interval-2" aria-hidden="true">#</a> <code>&lt;start_time_offset&gt;</code> &lt; <code>&lt;every_interval&gt;</code></h4><figure><img src="https://alioss.timecho.com/docs/img/UserGuide/Process-Data/Continuous-Query/pic3.png" alt="3" tabindex="0" loading="lazy"><figcaption>3</figcaption></figure><h4 id="every-interval-is-not-zero" tabindex="-1"><a class="header-anchor" href="#every-interval-is-not-zero" aria-hidden="true">#</a> <code>&lt;every_interval&gt;</code> is not zero</h4><figure><img src="https://alioss.timecho.com/docs/img/UserGuide/Process-Data/Continuous-Query/pic4.png" alt="4" tabindex="0" loading="lazy"><figcaption>4</figcaption></figure><ul><li><code>TIMEOUT POLICY</code> 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 <code>BLOCKED</code>. <ul><li><code>BLOCKED</code> means that we will block and wait to do the current cq execution task until the previous time interval cq task finishes. If using <code>BLOCKED</code> policy, all the time intervals will be executed, but it may be behind the latest time interval.</li><li><code>DISCARD</code> 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 <code>DISCARD</code> policy, some time intervals won&#39;t be executed when the execution time of one cq task is longer than the <code>&lt;every_interval&gt;</code>. 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.</li></ul></li></ul><h2 id="examples-of-cq" tabindex="-1"><a class="header-anchor" href="#examples-of-cq" aria-hidden="true">#</a> Examples of CQ</h2><p>The examples below use the following sample data. It&#39;s a real time data stream and we can assume that the data arrives on time.</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
| Time|root.ln.wf02.wt02.temperature|root.ln.wf02.wt01.temperature|root.ln.wf01.wt02.temperature|root.ln.wf01.wt01.temperature|
+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
|2021-05-11T22:18:14.598+08:00| 121.0| 72.0| 183.0| 115.0|
|2021-05-11T22:18:19.941+08:00| 0.0| 68.0| 68.0| 103.0|
|2021-05-11T22:18:24.949+08:00| 122.0| 45.0| 11.0| 14.0|
|2021-05-11T22:18:29.967+08:00| 47.0| 14.0| 59.0| 181.0|
|2021-05-11T22:18:34.979+08:00| 182.0| 113.0| 29.0| 180.0|
|2021-05-11T22:18:39.990+08:00| 42.0| 11.0| 52.0| 19.0|
|2021-05-11T22:18:44.995+08:00| 78.0| 38.0| 123.0| 52.0|
|2021-05-11T22:18:49.999+08:00| 137.0| 172.0| 135.0| 193.0|
|2021-05-11T22:18:55.003+08:00| 16.0| 124.0| 183.0| 18.0|
+-----------------------------+-----------------------------+-----------------------------+-----------------------------+-----------------------------+
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="configuring-execution-intervals" tabindex="-1"><a class="header-anchor" href="#configuring-execution-intervals" aria-hidden="true">#</a> Configuring execution intervals</h3><p>Use an <code>EVERY</code> interval in the <code>RESAMPLE</code> clause to specify the CQ’s execution interval, if not specific, default value is equal to <code>group_by_interval</code>.</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">CREATE</span> CONTINUOUS QUERY cq1
RESAMPLE EVERY <span class="token number">20</span>s
<span class="token keyword">BEGIN</span>
<span class="token keyword">SELECT</span> max_value<span class="token punctuation">(</span>temperature<span class="token punctuation">)</span>
<span class="token keyword">INTO</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">.</span><span class="token operator">*</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span><span class="token punctuation">(</span><span class="token number">10</span>s<span class="token punctuation">)</span>
<span class="token keyword">END</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq1</code> calculates the 10-second average of <code>temperature</code> sensor under the <code>root.ln</code> prefix path and stores the results in the <code>temperature_max</code> sensor using the same prefix path as the corresponding sensor.</p><p><code>cq1</code> executes at 20-second intervals, the same interval as the <code>EVERY</code> interval. Every 20 seconds, <code>cq1</code> runs a single query that covers the time range for the current time bucket, that is, the 20-second time bucket that intersects with <code>now()</code>.</p><p>Supposing that the current time is <code>2021-05-11T22:18:40.000+08:00</code>, we can see annotated log output about <code>cq1</code> running at DataNode if you set log level to DEBUG:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq1</code> won&#39;t deal with data that is before the current time window which is <code>2021-05-11T22:18:20.000+08:00</code>, so here are the results:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>&gt; 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: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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="configuring-time-range-for-resampling" tabindex="-1"><a class="header-anchor" href="#configuring-time-range-for-resampling" aria-hidden="true">#</a> Configuring time range for resampling</h3><p>Use <code>start_time_offset</code> in the <code>RANGE</code> clause to specify the start time of the CQ’s time range, if not specific, default value is equal to <code>EVERY</code> interval.</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">CREATE</span> CONTINUOUS QUERY cq2
RESAMPLE RANGE <span class="token number">40</span>s
<span class="token keyword">BEGIN</span>
<span class="token keyword">SELECT</span> max_value<span class="token punctuation">(</span>temperature<span class="token punctuation">)</span>
<span class="token keyword">INTO</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">.</span><span class="token operator">*</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span><span class="token punctuation">(</span><span class="token number">10</span>s<span class="token punctuation">)</span>
<span class="token keyword">END</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq2</code> calculates the 10-second average of <code>temperature</code> sensor under the <code>root.ln</code> prefix path and stores the results in the <code>temperature_max</code> sensor using the same prefix path as the corresponding sensor.</p><p><code>cq2</code> executes at 10-second intervals, the same interval as the <code>group_by_interval</code>. Every 10 seconds, <code>cq2</code> runs a single query that covers the time range between <code>now()</code> minus the <code>start_time_offset</code> and <code>now()</code> , that is, the time range between 40 seconds prior to <code>now()</code> and <code>now()</code>.</p><p>Supposing that the current time is <code>2021-05-11T22:18:40.000+08:00</code>, we can see annotated log output about <code>cq2</code> running at DataNode if you set log level to DEBUG:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq2</code> won&#39;t write lines that are all null. Notice <code>cq2</code> will also calculate the results for some time interval many times. Here are the results:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>&gt; 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="configuring-execution-intervals-and-cq-time-ranges" tabindex="-1"><a class="header-anchor" href="#configuring-execution-intervals-and-cq-time-ranges" aria-hidden="true">#</a> Configuring execution intervals and CQ time ranges</h3><p>Use an <code>EVERY</code> interval and <code>RANGE</code> interval in the <code>RESAMPLE</code> clause to specify the CQ’s execution interval and the length of the CQ’s time range. And use <code>fill()</code> to change the value reported for time intervals with no data.</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">CREATE</span> CONTINUOUS QUERY cq3
RESAMPLE EVERY <span class="token number">20</span>s RANGE <span class="token number">40</span>s
<span class="token keyword">BEGIN</span>
<span class="token keyword">SELECT</span> max_value<span class="token punctuation">(</span>temperature<span class="token punctuation">)</span>
<span class="token keyword">INTO</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">.</span><span class="token operator">*</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span><span class="token punctuation">(</span><span class="token number">10</span>s<span class="token punctuation">)</span>
FILL<span class="token punctuation">(</span><span class="token number">100.0</span><span class="token punctuation">)</span>
<span class="token keyword">END</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq3</code> calculates the 10-second average of <code>temperature</code> sensor under the <code>root.ln</code> prefix path and stores the results in the <code>temperature_max</code> sensor using the same prefix path as the corresponding sensor. Where possible, it writes the value <code>100.0</code> for time intervals with no results.</p><p><code>cq3</code> executes at 20-second intervals, the same interval as the <code>EVERY</code> interval. Every 20 seconds, <code>cq3</code> runs a single query that covers the time range between <code>now()</code> minus the <code>start_time_offset</code> and <code>now()</code>, that is, the time range between 40 seconds prior to <code>now()</code> and <code>now()</code>.</p><p>Supposing that the current time is <code>2021-05-11T22:18:40.000+08:00</code>, we can see annotated log output about <code>cq3</code> running at DataNode if you set log level to DEBUG:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Notice that <code>cq3</code> will calculate the results for some time interval many times, so here are the results:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>&gt; 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="configuring-end-time-offset-for-cq-time-range" tabindex="-1"><a class="header-anchor" href="#configuring-end-time-offset-for-cq-time-range" aria-hidden="true">#</a> Configuring end_time_offset for CQ time range</h3><p>Use an <code>EVERY</code> interval and <code>RANGE</code> interval in the RESAMPLE clause to specify the CQ’s execution interval and the length of the CQ’s time range. And use <code>fill()</code> to change the value reported for time intervals with no data.</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">CREATE</span> CONTINUOUS QUERY cq4
RESAMPLE EVERY <span class="token number">20</span>s RANGE <span class="token number">40</span>s<span class="token punctuation">,</span> <span class="token number">20</span>s
<span class="token keyword">BEGIN</span>
<span class="token keyword">SELECT</span> max_value<span class="token punctuation">(</span>temperature<span class="token punctuation">)</span>
<span class="token keyword">INTO</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf02<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt02<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span><span class="token punctuation">,</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span>wf01<span class="token punctuation">.</span>wt01<span class="token punctuation">(</span>temperature_max<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">.</span><span class="token operator">*</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span><span class="token punctuation">(</span><span class="token number">10</span>s<span class="token punctuation">)</span>
FILL<span class="token punctuation">(</span><span class="token number">100.0</span><span class="token punctuation">)</span>
<span class="token keyword">END</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq4</code> calculates the 10-second average of <code>temperature</code> sensor under the <code>root.ln</code> prefix path and stores the results in the <code>temperature_max</code> sensor using the same prefix path as the corresponding sensor. Where possible, it writes the value <code>100.0</code> for time intervals with no results.</p><p><code>cq4</code> executes at 20-second intervals, the same interval as the <code>EVERY</code> interval. Every 20 seconds, <code>cq4</code> runs a single query that covers the time range between <code>now()</code> minus the <code>start_time_offset</code> and <code>now()</code> minus the <code>end_time_offset</code>, that is, the time range between 40 seconds prior to <code>now()</code> and 20 seconds prior to <code>now()</code>.</p><p>Supposing that the current time is <code>2021-05-11T22:18:40.000+08:00</code>, we can see annotated log output about <code>cq4</code> running at DataNode if you set log level to DEBUG:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
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:
&gt;
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
| 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
&gt;
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Notice that <code>cq4</code> will calculate the results for all time intervals only once after a delay of 20 seconds, so here are the results:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>&gt; 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|
+-----------------------------+---------------------------------+---------------------------------+---------------------------------+---------------------------------+
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="cq-without-group-by-clause" tabindex="-1"><a class="header-anchor" href="#cq-without-group-by-clause" aria-hidden="true">#</a> CQ without group by clause</h3><p>Use an <code>EVERY</code> interval in the <code>RESAMPLE</code> clause to specify the CQ’s execution interval and the length of the CQ’s time range.</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">CREATE</span> CONTINUOUS QUERY cq5
RESAMPLE EVERY <span class="token number">20</span>s
<span class="token keyword">BEGIN</span>
<span class="token keyword">SELECT</span> temperature <span class="token operator">+</span> <span class="token number">1</span>
<span class="token keyword">INTO</span> root<span class="token punctuation">.</span>precalculated_sg<span class="token punctuation">.</span>::<span class="token punctuation">(</span>temperature<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> root<span class="token punctuation">.</span>ln<span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">.</span><span class="token operator">*</span>
align <span class="token keyword">by</span> device
<span class="token keyword">END</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq5</code> calculates the <code>temperature + 1</code> under the <code>root.ln</code> prefix path and stores the results in the <code>root.precalculated_sg</code> database. Sensors use the same prefix path as the corresponding sensor.</p><p><code>cq5</code> executes at 20-second intervals, the same interval as the <code>EVERY</code> interval. Every 20 seconds, <code>cq5</code> runs a single query that covers the time range for the current time bucket, that is, the 20-second time bucket that intersects with <code>now()</code>.</p><p>Supposing that the current time is <code>2021-05-11T22:18:40.000+08:00</code>, we can see annotated log output about <code>cq5</code> running at DataNode if you set log level to DEBUG:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>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:
&gt;
+-----------------------------+-------------------------------+-----------+
| 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|
+-----------------------------+-------------------------------+-----------+
&gt;
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:
&gt;
+-----------------------------+-------------------------------+-----------+
| 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|
+-----------------------------+-------------------------------+-----------+
&gt;
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code>cq5</code> won&#39;t deal with data that is before the current time window which is <code>2021-05-11T22:18:20.000+08:00</code>, so here are the results:</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>&gt; 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|
+-----------------------------+-------------------------------+-----------+
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="cq-management" tabindex="-1"><a class="header-anchor" href="#cq-management" aria-hidden="true">#</a> CQ Management</h2><h3 id="listing-continuous-queries" tabindex="-1"><a class="header-anchor" href="#listing-continuous-queries" aria-hidden="true">#</a> Listing continuous queries</h3><p>List every CQ on the IoTDB Cluster with:</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">SHOW</span> <span class="token punctuation">(</span>CONTINUOUS QUERIES <span class="token operator">|</span> CQS<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p><code>SHOW (CONTINUOUS QUERIES | CQS)</code> order results by <code>cq_id</code>.</p><h4 id="examples" tabindex="-1"><a class="header-anchor" href="#examples" aria-hidden="true">#</a> Examples</h4><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">SHOW</span> CONTINUOUS QUERIES<span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>we will get:</p><table><thead><tr><th style="text-align:left;">cq_id</th><th>query</th><th>state</th></tr></thead><tbody><tr><td style="text-align:left;">s1_count_cq</td><td>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</td><td>active</td></tr></tbody></table><h3 id="dropping-continuous-queries" tabindex="-1"><a class="header-anchor" href="#dropping-continuous-queries" aria-hidden="true">#</a> Dropping continuous queries</h3><p>Drop a CQ with a specific <code>cq_id</code>:</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">DROP</span> <span class="token punctuation">(</span>CONTINUOUS QUERY <span class="token operator">|</span> CQ<span class="token punctuation">)</span> <span class="token operator">&lt;</span>cq_id<span class="token operator">&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>DROP CQ returns an empty result.</p><h4 id="examples-1" tabindex="-1"><a class="header-anchor" href="#examples-1" aria-hidden="true">#</a> Examples</h4><p>Drop the CQ named <code>s1_count_cq</code>:</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">DROP</span> CONTINUOUS QUERY s1_count_cq<span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h3 id="altering-continuous-queries" tabindex="-1"><a class="header-anchor" href="#altering-continuous-queries" aria-hidden="true">#</a> Altering continuous queries</h3><p>CQs can&#39;t be altered once they&#39;re created. To change a CQ, you must <code>DROP</code> and re<code>CREATE</code> it with the updated settings.</p><h2 id="cq-use-cases" tabindex="-1"><a class="header-anchor" href="#cq-use-cases" aria-hidden="true">#</a> CQ Use Cases</h2><h3 id="downsampling-and-data-retention" tabindex="-1"><a class="header-anchor" href="#downsampling-and-data-retention" aria-hidden="true">#</a> Downsampling and Data Retention</h3><p>Use CQs with <code>TTL</code> set on database in IoTDB to mitigate storage concerns. Combine CQs and <code>TTL</code> to automatically downsample high precision data to a lower precision and remove the dispensable, high precision data from the database.</p><h3 id="recalculating-expensive-queries" tabindex="-1"><a class="header-anchor" href="#recalculating-expensive-queries" aria-hidden="true">#</a> Recalculating expensive queries</h3><p>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.</p><blockquote><p>Pre-calculate queries for your preferred graphing tool to accelerate the population of graphs and dashboards.</p></blockquote><h3 id="substituting-for-sub-query" tabindex="-1"><a class="header-anchor" href="#substituting-for-sub-query" aria-hidden="true">#</a> Substituting for sub-query</h3><p>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.</p><h4 id="example" tabindex="-1"><a class="header-anchor" href="#example" aria-hidden="true">#</a> Example</h4><p>IoTDB does not accept the following query with a nested sub query. The query calculates the average number of non-null values of <code>s1</code> at 30 minute intervals:</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">SELECT</span> <span class="token function">avg</span><span class="token punctuation">(</span>count_s1<span class="token punctuation">)</span> <span class="token keyword">from</span> <span class="token punctuation">(</span><span class="token keyword">select</span> <span class="token function">count</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span> <span class="token keyword">as</span> count_s1 <span class="token keyword">from</span> root<span class="token punctuation">.</span>sg<span class="token punctuation">.</span>d <span class="token keyword">group</span> <span class="token keyword">by</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">30</span>m<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>To get the same results:</p><p><strong>1. Create a CQ</strong></p><p>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 <code>s1</code> at 30 minute intervals and writes those counts into the new <code>root.sg_count.d.count_s1</code> time series.</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">CREATE</span> CQ s1_count_cq
<span class="token keyword">BEGIN</span>
<span class="token keyword">SELECT</span> <span class="token function">count</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span>
<span class="token keyword">INTO</span> root<span class="token punctuation">.</span>sg_count<span class="token punctuation">.</span>d<span class="token punctuation">(</span>count_s1<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> root<span class="token punctuation">.</span>sg<span class="token punctuation">.</span>d
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span><span class="token punctuation">(</span><span class="token number">30</span>m<span class="token punctuation">)</span>
<span class="token keyword">END</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>2. Query the CQ results</strong></p><p>Next step performs the avg([...]) part of the outer query above.</p><p>Query the data in the time series <code>root.sg_count.d.count_s1</code> to calculate the average of it:</p><div class="language-sql line-numbers-mode" data-ext="sql"><pre class="language-sql"><code><span class="token keyword">SELECT</span> <span class="token function">avg</span><span class="token punctuation">(</span>count_s1<span class="token punctuation">)</span> <span class="token keyword">from</span> root<span class="token punctuation">.</span>sg_count<span class="token punctuation">.</span>d<span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="system-parameter-configuration" tabindex="-1"><a class="header-anchor" href="#system-parameter-configuration" aria-hidden="true">#</a> System Parameter Configuration</h2><table><thead><tr><th style="text-align:left;">Name</th><th>Description</th><th>Data Type</th><th>Default Value</th></tr></thead><tbody><tr><td style="text-align:left;"><code>continuous_query_submit_thread</code></td><td>The number of threads in the scheduled thread pool that submit continuous query tasks periodically</td><td>int32</td><td>2</td></tr><tr><td style="text-align:left;"><code>continuous_query_min_every_interval_in_ms</code></td><td>The minimum value of the continuous query execution time interval</td><td>duration</td><td>1000</td></tr></tbody></table>`,97);function T(q,E){const a=o("ExternalLinkIcon");return l(),r("div",null,[c(`
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
`),u,e("ul",null,[p,m,e("li",null,[v,n(" specifies the start time of each query execution as "),h,n(". We currently support the units of ns, us, ms, s, m, h, d, "),e("a",b,[n("w.It"),t(a)]),n("'s an optional parameter, default value is set to "),g,n(" in resample clause.")]),e("li",null,[w,n(" specifies the end time of each query execution as "),k,n(". We currently support the units of ns, us, ms, s, m, h, d, "),e("a",f,[n("w.It"),t(a)]),n("'s an optional parameter, default value is set to "),_,n(".")]),x]),y])}const R=i(d,[["render",T],["__file","Continuous-Query.html.vue"]]);export{R as default};