2. InfluxDB关键概念

InfluxDB前篇介绍

Centos7 下 InfluxDB 从安装开始到入门

前一篇根据InfluxDB的官方开源文档进行了一次实践。这篇来继续看看InfluxDB的关键概念。

喜欢看英文开源文档的,可以访问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 在不同时间点以及不同地点记录蝴蝶和蜜蜂的数量。

将样本数据插入到influxDB中

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显示与特定数据关联的日期和时间。

  • butterflieshoneybees字段(fields):这两个字段(fields)由字段键(field keys)和字段值(field values)组成。
    字段键(field keys) : butterflieshoneybees 则是表的字段名;
    字段值(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)locationscientist
    示例数据中的最后两列(locationscientist)是标记。
    标签由标签键和标签值组成。
    标签键标记值存储为字符串和记录元数据。
    示例数据中的标记键是locationscientist
    标记键location有两个标记值:12
    标记键scientist还有两个标记值:langstrothperpetua

在上面的数据中, 标记集是所有标记键值对的不同组合。样本数据中的四个标记集是:

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,属于索引,查询大规模数据的时候效率就会提升。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • 什么是InfluxDB? InfluxDB介绍 InfluxDB是一款用Go语言编写的开源分布式时序、事件和指标数...
    BilyLuo阅读 3,248评论 0 18
  • InfluxDB是一个开源的时序数据库,使用GO语言开发,特别适合用于处理和分析资源监控数据这种时序相关数据。而I...
    Boris0621阅读 2,974评论 0 8
  • InfluxDB是一个开源的时序数据库,使用GO语言开发,特别适合用于处理和分析资源监控数据这种时序相关数据。而I...
    __七把刀__阅读 163,480评论 19 90
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 31,790评论 2 89
  • [我想,我认识你的时候一定不知道后来我们会那么好。] 大二接新生,那个时候的我,比现在害羞,比现在更没自信。...
    温玉生烟阅读 239评论 0 0