本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/)
OFFSET 和SOFFSET对返回的points和series进行分页。
一、OFFSET子句
OFFSET <N> 将从查询结果的第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表示从查询结果的第N个points开始进行分页。注意OFFSET必须和LIMIT搭配使用,如果只有OFFSET而没有LIMIT,将会导致不一致的查询结果。
OFFSET示例sql
- 示例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行显示了出来。
- 示例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> 将从查询结果的第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>中的N指定了开始分页的地方,SOFFSET要跟SLIMIT子句一同搭配使用。如果只使用SOFFSET而没有SLIMIT子句,则可能会导致不一致的查询结果。
SOFFSET示例sql
- 示例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开始的)。
- 示例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的数据。