前面一篇介紹了influxdb中基本的查詢操作,在結尾處提到了如果我們希望對查詢的結果進行分組,排序,分頁時,應該怎么操作,接下來我們看一下上面幾個場景的支持
在開始本文之前,建議先閱讀上篇博文: 190813-Influx Sql系列教程八:query數據查詢基本篇
0. 數據准備
在開始查詢之前,先看一下我們准備的數據,其中name,phone
為tag, age,blog,id
為field
> select * from yhh
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
1563889538654374538 26 http://blog.hhui.top 10 一灰灰
1563889547738266214 30 http://blog.hhui.top 11 一灰灰
1563889704754695002 30 http://blog.hhui.top 11 一灰灰2
1563889723440000821 30 http://blog.hhui.top 11 一灰灰3 110
> show tag keys from yhh
name: yhh
tagKey
------
name
phone
1. 分組查詢
和sql語法一樣,influxdb sql的分組也是使用group by
語句,其定義如下
SELECT_clause FROM_clause [WHERE_clause] GROUP BY [* | <tag_key>[,<tag_key]]
a. group by tag
從上面的定義中,有一點需要特別強調,用來分組的必須是tag,也就是說對於influxdb而言,不支持根據field進行分組
一個實際的演示如下:
> select * from yhh group by phone
name: yhh
tags: phone=
time age blog id name
---- --- ---- -- ----
1563889538654374538 26 http://blog.hhui.top 10 一灰灰
1563889547738266214 30 http://blog.hhui.top 11 一灰灰
1563889704754695002 30 http://blog.hhui.top 11 一灰灰2
name: yhh
tags: phone=110
time age blog id name
---- --- ---- -- ----
1563889723440000821 30 http://blog.hhui.top 11 一灰灰3
注意上面的輸出結果,比較有意思,分成了兩個結構段落,且可以輸出完整的數據;而mysql的分組查詢條件中一般需要帶上分組key,然后實現一些數據上的聚合查詢
如果我的分組中,使用field進行分組查詢,會怎樣?報錯么?
> select * from yhh group by age
name: yhh
tags: age=
time age blog id name phone
---- --- ---- -- ---- -----
1563889538654374538 26 http://blog.hhui.top 10 一灰灰
1563889547738266214 30 http://blog.hhui.top 11 一灰灰
1563889704754695002 30 http://blog.hhui.top 11 一灰灰2
1563889723440000821 30 http://blog.hhui.top 11 一灰灰3 110
從上面的case中可以看出,雖然執行了,但是返回的結果並不是我們預期的。
b. group by *
另外一個與一般SQL語法不一樣的是group by
后面可以跟上*
,表示根據所有的tag進行分組,一個測試如下
> select * from yhh group by *
name: yhh
tags: name=一灰灰, phone=
time age blog id
---- --- ---- --
1563889538654374538 26 http://blog.hhui.top 10
1563889547738266214 30 http://blog.hhui.top 11
name: yhh
tags: name=一灰灰2, phone=
time age blog id
---- --- ---- --
1563889704754695002 30 http://blog.hhui.top 11
name: yhh
tags: name=一灰灰3, phone=110
time age blog id
---- --- ---- --
1563889723440000821 30 http://blog.hhui.top 11
>
c. group by time
除了上面的根據tag進行分組之外,還有一個更高級的特性,根據時間來分組,這個時間還支持一些簡單的函數操作
定義如下
SELECT <function>(<field_key>) FROM_clause WHERE <time_range> GROUP BY time(<time_interval>),[tag_key] [fill(<fill_option>)]
我們知道influxdb的一個重要應用場景就是監控的記錄,在監控面板上經常會有的就是根據時間進行聚合,比如查詢某個服務每分鍾的異常數,qps, rt等
下面給出一個簡單的使用case
# 為了顯示方便,將數據的時間戳改成日期方式展示
> precision rfc3339
> select * from yhh
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
> select count(*) from yhh where time>'2019-07-23T13:44:38.654374538Z' and time<'2019-07-23T13:50:43.440000821Z' GROUP BY time(2m)
name: yhh
time count_age count_blog count_id
---- --------- ---------- --------
2019-07-23T13:44:00Z 2 2 2
2019-07-23T13:46:00Z 0 0 0
2019-07-23T13:48:00Z 2 2 2
2019-07-23T13:50:00Z 0 0 0
在上面的查詢語句中,有幾個地方需要說明一下
- select后面跟上的是單個or多個field的聚合操作,根據時間進行分組時,不允許查詢具體的field值,否則會有下面的錯誤提示
> select * from yhh where time>'2019-07-23T13:44:38.654374538Z' and time<'2019-07-23T13:50:43.440000821Z' GROUP BY time(2m) ERR: GROUP BY requires at least one aggregate function
- where條件限定查詢的時間范圍,否則會得到很多數據
group by time(2m)
表示每2分鍾做一個分組,group by time(2s)
則表示每2s做一個分組
2. 排序
在influxdb中排序,只支持針對time進行排序,其他的field,tag(因為是string類型,也沒法排)是不能進行排序的
語法比較簡單,如下,根據時間倒序/升序
order by time desc/asc
一個簡單的實例如下
# 根據非time進行排序時,直接報錯
> select * from yhh order by age
ERR: error parsing query: only ORDER BY time supported at this time
# 根據時間進行倒排
> select * from yhh order by time desc
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
>
3. 查詢限制
我們常見的分頁就是limit語句,我們常見的limit語句為 limit page, size
,可以實現分頁;然而在influxdb中則不同,limit后面只能跟上一個數字,表示限定查詢的最多條數
a. limit
N指定每次measurement返回的point個數
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] LIMIT <N>
下滿給出幾個實際的case
> select * from yhh limit 2
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
# 分組之后,再限定查詢條數
> select * from yhh group by "name" limit 1
name: yhh
tags: name=一灰灰
time age blog id phone
---- --- ---- -- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10
name: yhh
tags: name=一灰灰2
time age blog id phone
---- --- ---- -- -----
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11
name: yhh
tags: name=一灰灰3
time age blog id phone
---- --- ---- -- -----
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 110
b. slimit
N指定從指定measurement返回的series數
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] GROUP BY *[,time(<time_interval>)] [ORDER_BY_clause] SLIMIT <N>
接下來演示下這個的使用姿勢,首先准備插入幾條數據,確保tag相同
> insert yhh,name=一灰灰,phone=110 blog="http://spring.hhui.top",age=14,id=14
> insert yhh,name=一灰灰,phone=110 blog="http://spring.hhui.top",age=15,id=15
> insert yhh,name=一灰灰,phone=110 blog="http://spring.hhui.top",age=16,id=16
> select * from yhh
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14 一灰灰 110
2019-08-14T11:18:10.146588721Z 15 http://spring.hhui.top 15 一灰灰 110
2019-08-14T11:18:12.753413004Z 16 http://spring.hhui.top 16 一灰灰 110
> show series on test from yhh
key
---
yhh,name=一灰灰
yhh,name=一灰灰,phone=110
yhh,name=一灰灰2
yhh,name=一灰灰3,phone=110
如下面的一個使用case
> select * from yhh group by * slimit 3
name: yhh
tags: name=一灰灰, phone=
time age blog id
---- --- ---- --
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11
name: yhh
tags: name=一灰灰, phone=110
time age blog id
---- --- ---- --
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14
2019-08-14T11:18:10.146588721Z 15 http://spring.hhui.top 15
2019-08-14T11:18:12.753413004Z 16 http://spring.hhui.top 16
name: yhh
tags: name=一灰灰2, phone=
time age blog id
---- --- ---- --
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11
name: yhh
tags: name=一灰灰3, phone=110
time age blog id
---- --- ---- --
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11
說實話,這一塊沒看懂,根據官方的文檔進行翻譯的,沒有get這個slimit的特點
4. 分頁
上面只有point個數限制,但是分頁怎么辦?難道不支持么?
在influxdb中,有專門的offset來實現分頁
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] LIMIT_clause OFFSET <N> [SLIMIT_clause]
簡單來講,就是limit 條數 offset 偏移
使用實例
> select * from yhh
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14 一灰灰 110
2019-08-14T11:18:10.146588721Z 15 http://spring.hhui.top 15 一灰灰 110
2019-08-14T11:18:12.753413004Z 16 http://spring.hhui.top 16 一灰灰 110
# 查詢結果只有2條數據,從第三個開始(0開始計數)
> select * from yhh limit 2 offset 3
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14 一灰灰 110
> select * from yhh limit 2 offset 3
5. 小結
本篇influxdb的查詢篇主要介紹了sql中的三種常用case,分組,排序,分頁;雖然使用姿勢和我們常見的SQL大同小異,但是一些特殊點需要額外注意一下
- 分組查詢時,注意分組的key必須是time或者tag,分組查詢可以返回完整的point
- 排序,只支持根據時間進行排序,其他的字段都不支持
- 分頁,需要注意
limit size offset startIndex
和我們一般的使用case不同,它的兩個參數分別表示查詢的point個數,以及偏移量;而不是傳統sql中的頁和條數
II. 其他
0. 系列博文
- 190813-Influx Sql系列教程八:query數據查詢基本篇
- 190730-Influx Sql系列教程七:delete 刪除數據
- 190729-Influx Sql系列教程六:insert 修改數據
- 190726-Influx Sql系列教程五:insert 添加數據
- 190723-Influx Sql系列教程四:series/point/tag/field
- 190721-Influx Sql系列教程三:measurement 表
- 190719-Influx Sql系列教程二:retention policy 保存策略
- 190718-Influx Sql系列教程一:database 數據庫
- 190717-Influx Sql系列教程零:安裝及influx-cli使用姿勢介紹
- 190509-InfluxDb之時間戳顯示為日期格式
- 190506-InfluxDB之配置修改
- 190505-InfluxDB之權限管理
- 180727-時序數據庫InfluxDB之備份和恢復策略
- 180726-InfluxDB基本概念小結
- 180725-InfluxDB-v1.6.0安裝和簡單使用小結
參考博文
1. 一灰灰Blog: https://liuyueyi.github.io/hexblog
一灰灰的個人博客,記錄所有學習和工作中的博文,歡迎大家前去逛逛
2. 聲明
盡信書則不如,已上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
- 微博地址: 小灰灰Blog
- QQ: 一灰灰/3302797840
3. 掃描關注
一灰灰blog