InfluxDB-InfluxQL基礎語法教程--OFFSET 和SOFFSET子句

本文翻譯自官網,官網地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/)

OFFSET 和SOFFSET對返回的points和series進行分頁。

一、OFFSET子句

OFFSET 將從查詢結果的第N個points開始進行分頁。
語法:

SELECT_clause [INTO_clause] FROM_clause
[WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause]
LIMIT_clause OFFSET <N>
[SLIMIT_clause]

OFFSET中的N表示從查詢結果的第N個points開始進行分頁。注意OFFSET必須和LIMIT搭配使用,如果只有OFFSET而沒有LIMIT,將會導致不一致的查詢結果。

OFFSET示例sql

  1. 示例1
    爲了對比效果更明顯,我們先看下面的sql

上面的sql查詢除了measurement的前10行
接下來看下面的sql

SELECT "water_level","location" FROM "h2o_feet"
LIMIT 3 OFFSET 4


可以看到,LIMIT 3 OFFSET 4將查詢結果的從下標4開始的第5、6、7行總共3行顯示了出來。

  1. 示例2
    Sql
SELECT MEAN("water_level") FROM "h2o_feet"
WHERE time >= '2015-08-18T00:00:00Z'
    AND time <= '2015-08-18T00:42:00Z'
GROUP BY *,time(12m)
ORDER BY time DESC
LIMIT 2 OFFSET 2 SLIMIT 1


這個例子非常複雜,下面是逐條分解:

  • The SELECT clause specifies an InfluxQL function.
  • The FROM clause specifies a single measurement.
  • The WHERE clause specifies the time range for the query.
  • The GROUP BY clause groups results by all tags (*) and into 12-minute intervals.
  • The ORDER BY time DESC clause returns results in descending timestamp order.
  • The LIMIT 2 clause limits the number of points returned to two.
  • The OFFSET 2 clause excludes the first two averages from the query results.
  • The SLIMIT 1 clause limits the number of series returned to one.
  • The SOFFSET 1 clause paginates the series returned.

如果上面的sql中沒有SOFFSET 2,則會查詢到不同的series:


二、SOFFSET子句

SOFFSET 將從查詢結果的第N個series開始進行分頁。
語法:

SELECT_clause [INTO_clause] FROM_clause [WHERE_clause]
GROUP BY *[,time(time_interval)]
[ORDER_BY_clause]
[LIMIT_clause] [OFFSET_clause]
SLIMIT_clause SOFFSET <N>

SOFFSET 中的N指定了開始分頁的地方,SOFFSET要跟SLIMIT子句一同搭配使用。如果只使用SOFFSET而沒有SLIMIT子句,則可能會導致不一致的查詢結果。


SOFFSET示例sql

  1. 示例1

    上面的sql將h2o_feet表中tag的 location = santa_monica的所有數據。如果沒有SOFFSET 1子句,查詢結果將會變成是location = coyote_creek的數據。爲了更好的說明這個問題,依次看下面的示例。
SELECT count("water_level") FROM "h2o_feet" GROUP BY *


可以看到,上面的sql查詢出每個tag的water_level字段個數。
讓我們在上面sql的基礎上,加上SLIMIT 1:

因爲加上了SLIMIT 1,所以查詢結果只展示了第一個tag的結果。
再在上面sql的基礎上加上SOFFSET 1:

可見,因爲加上了SOFFSET 1,所以查詢結果從第二個series開始展示(下標是從0開始的)。

  1. 示例2
    接下來看一個更復雜的sql
SELECT MEAN("water_level") FROM "h2o_feet"
WHERE time >= '2015-08-18T00:00:00Z'
    AND time <= '2015-08-18T00:42:00Z'
GROUP BY *,time(12m)
ORDER BY time DESC
LIMIT 2 OFFSET 2
SLIMIT 1 SOFFSET 1

示例sql相對比較複雜,下面將逐個子句的進行分析(挺簡單的,不翻譯了):

  • The SELECT clause specifies an InfluxQL function.
  • The FROM clause specifies a single measurement.
  • The WHERE clause specifies the time range for the query.
  • The GROUP BY clause groups results by all tags (*) and into 12-minute intervals.
  • The ORDER BY time DESC clause returns results in descending timestamp order.
  • The LIMIT 2 clause limits the number of points returned to two.
  • The OFFSET 2 clause excludes the first two averages from the query results.
  • The SLIMIT 1 clause limits the number of series returned to one.
  • The SOFFSET 1 clause paginates the series returned.
    如果沒有SOFFSET 1,查詢結果將會是:

可以看到,查詢到的是另一個series的數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章