InfluxDB前篇介紹
Centos7 下 InfluxDB 從安裝開始到入門web
前一篇根據InfluxDB的官方開源文檔進行了一次實踐。這篇來繼續看看InfluxDB的關鍵概念。shell
喜歡看英文開源文檔的,能夠訪問InfluxDB key concepts,直接閱讀關鍵概念。數據庫
若是不喜歡直接看英文的,就繼續看我下面的翻譯後描述吧。微信
InfluxDB的關鍵概念
在深刻了解InfluxDB以前,熟悉數據庫的一些關鍵概念是很好的。本文檔簡要介紹了這些概念和通用的InfluxDB術語。下面列出了涵蓋的全部術語,但建議您從頭至尾閱讀本文檔,以便更全面地瞭解咱們最喜歡的時間序列數據庫。數據結構
database | field key | field set |
---|---|---|
field value | measurement | point |
retention policy | series | tag key |
tag set | tag value | timestamp |
若是想要更加詳細地瞭解術語的定義,請查看術語表。編輯器
樣本數據 Sample data
name: census(普查)優化
time | butterflies | honeybees | location | scientist |
---|---|---|---|---|
2015-08-18T00:00:00Z | 12 | 23 | 1 | langstroth |
2015-08-18T00:00:00Z | 1 | 30 | 1 | perpetua |
2015-08-18T00:06:00Z | 11 | 28 | 1 | langstroth |
2015-08-18T00:06:00Z | 3 | 28 | 1 | perpetua |
2015-08-18T05:54:00Z | 2 | 11 | 2 | langstroth |
2015-08-18T06:00:00Z | 1 | 10 | 2 | langstroth |
2015-08-18T06:06:00Z | 8 | 23 | 2 | perpetua |
2015-08-18T06:12:00Z | 7 | 22 | 2 | perpetua |
上面的樣例數據表示兩個科學家 langstroth 和 perpetua 在不一樣時間點以及不一樣地點記錄蝴蝶和蜜蜂的數量。spa
將樣本數據插入到influxDB中.net
root@d2918dc47850:/# influx
Connected to http://localhost:8086 version 1.7.2
InfluxDB shell version: 1.7.2
Enter an InfluxQL query
> show databases
name: databases
name
----
_internal
mydb
>
> use mydb
Using database mydb
>
>
> insert census,scientist=langstroth,location=1 butterflies=12,honeybees=23
> insert census,scientist=perpetua,location=1 butterflies=1,honeybees=30
> insert census,scientist=langstroth,location=1 butterflies=11,honeybees=28
> insert census,scientist=perpetua,location=1 butterflies=3,honeybees=28
> insert census,scientist=langstroth,location=2 butterflies=2,honeybees=11
> insert census,scientist=perpetua,location=2 butterflies=1,honeybees=10
> insert census,scientist=langstroth,location=2 butterflies=8,honeybees=23
> insert census,scientist=perpetua,location=2 butterflies=7,honeybees=22
>
> select * from census
name: census
time butterflies honeybees location scientist
---- ----------- --------- -------- ---------
1546741552382793960 12 23 1 langstroth
1546741591954384804 1 30 1 perpetua
1546741614036950839 11 28 1 langstroth
1546741636651092337 3 28 1 perpetua
1546741656423108444 2 11 2 langstroth
1546741670749604756 1 10 2 perpetua
1546741686055646710 8 23 2 langstroth
1546741704010462064 7 22 2 perpetua
>
樣本數據字段的含義說明
-
time : 在上面的數據中有一個名爲
time
- InfluxDB中的全部數據都有該列。time
存儲時間戳,以及timestamp以 RFC3339 UTC顯示與特定數據關聯的日期和時間。翻譯 -
butterflies
和honeybees
字段(fields
):這兩個字段(fields
)由字段鍵(field keys
)和字段值(field values
)組成。 字段鍵(field keys
) :butterflies
和honeybees
則是表的字段名;字段值(field values
):能夠是字符串,浮點數,整數或布爾值,而且因爲InfluxDB是時間序列數據庫,所以字段值始終與時間戳相關聯。
示例數據中的字段值爲:
12 23
1 30
11 28
3 28
2 11
1 10
8 23
7 22
在上面的數據中,字段鍵(field keys)和字段值(field values)對的集合構成了一個 字段集(field set)。如下是示例數據中的全部八個字段集:
butterflies = 12 honeybees = 23
butterflies = 1 honeybees = 30
butterflies = 11 honeybees = 28
butterflies = 3 honeybees = 28
butterflies = 2 honeybees = 11
butterflies = 1 honeybees = 10
butterflies = 8 honeybees = 23
butterflies = 7 honeybees = 22
字段(fields)是InfluxDB數據結構的必需部分。沒有字段,您不能在InfluxDB中擁有數據。一樣重要的是要注意:字段不能設置爲索引。 使用字段值做爲過濾器的查詢必須掃描與查詢中的其餘條件匹配的全部值,因此效率相對於標記(tag)查詢偏低。其中標記(tag)查詢能夠設置索引,因此查詢效率更高。
-
標記(tag) location
和scientist
:示例數據中的最後兩列(location
和scientist
)是標記。標籤由標籤鍵和標籤值組成。 標籤鍵和 標記值存儲爲字符串和記錄元數據。示例數據中的標記鍵是location
和scientist
。標記鍵location
有兩個標記值:1
和2
。標記鍵scientist
還有兩個標記值:langstroth
和perpetua
。
在上面的數據中, 標記集是全部標記鍵值對的不一樣組合。樣本數據中的四個標記集是:
location = 1, scientist = langstroth
location = 2, scientist = langstroth
location = 1, scientist = perpetua
location = 2, scientist = perpetua
標籤是可選的。您不須要在數據結構中包含標記,但一般最好使用它們,由於與字段不一樣,標記是索引的。這意味着對標籤的查詢更快,而且該標籤很是適合存儲經常使用查詢元數據。
查詢條件中,索引很重要
假設您注意到大多數查詢都關注字段鍵的值,honeybees、butterflies查詢語句以下:SELECT * FROM "census" WHERE "butterflies" = 1
SELECT * FROM "census" WHERE "honeybees" = 23
執行以下:
> SELECT * FROM "census" WHERE "butterflies" = 1
name: census
time butterflies honeybees location scientist
---- ----------- --------- -------- ---------
1546741591954384804 1 30 1 perpetua
1546741670749604756 1 10 2 perpetua
>
> SELECT * FROM "census" WHERE "honeybees" = 23
name: census
time butterflies honeybees location scientist
---- ----------- --------- -------- ---------
1546741552382793960 12 23 1 langstroth
1546741686055646710 8 23 2 langstroth
>
可是因爲字段鍵(field key) 是沒有索引的,在大規模數據查詢的時候會掃描全表數據,此時效率就會很低,那麼該如何去優化呢?
此時就應該將butterflies、honeybees
兩個字段設置爲tag
,而location、scientist
設置爲field
。
insert census,butterflies=1,honeybees=30 scientist="perpetua",location=1
insert census,butterflies=11,honeybees=28 scientist="langstroth",location=1
insert census,butterflies=3,honeybees=28 scientist="perpetua",location=1
insert census,butterflies=2,honeybees=11 scientist="langstroth",location=2
insert census,butterflies=1,honeybees=10 scientist="perpetua",location=2
insert census,butterflies=8,honeybees=23 scientist="langstroth",location=2
insert census,butterflies=7,honeybees=22 scientist="perpetua",location=2
insert census,butterflies=12,honeybees=23 scientist="langstroth",location=1
操做以下:
> use mydb
Using database mydb
>
## 查看有哪些表
> show measurements
name: measurements
name
----
census
cpu
temperature
## 清空表數據
> delete from census;
>
> select * from census;
>
## 插入數據
> insert census,butterflies=1,honeybees=30 scientist="perpetua",location=1
> insert census,butterflies=11,honeybees=28 scientist="langstroth",location=1
> insert census,butterflies=3,honeybees=28 scientist="perpetua",location=1
> insert census,butterflies=2,honeybees=11 scientist="langstroth",location=2
> insert census,butterflies=1,honeybees=10 scientist="perpetua",location=2
> insert census,butterflies=8,honeybees=23 scientist="langstroth",location=2
> insert census,butterflies=7,honeybees=22 scientist="perpetua",location=2
> insert census,butterflies=12,honeybees=23 scientist="langstroth",location=1
>
> select * from census
name: census
time butterflies honeybees location scientist
---- ----------- --------- -------- ---------
1546743438630926762 1 30 1 perpetua
1546743446986027738 11 28 1 langstroth
1546743446997025073 3 28 1 perpetua
1546743447019092699 2 11 2 langstroth
1546743447023970929 1 10 2 perpetua
1546743447027505445 8 23 2 langstroth
1546743447032866644 7 22 2 perpetua
1546743448855305845 12 23 1 langstroth
>
此時,butterflies honeybees
已是tag,屬於索引,查詢大規模數據的時候效率就會提高。
本文分享自微信公衆號 - DevOps社羣(DevOpsCommunity)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。