关键概念
在深入InfluxDB之前,最好是了解数据库的一些关键概念。 本文档简要介绍了这些概念和常用的InfluxDB术语。 我们在下面列出了所有涵盖的术语,但是我们建议您从头到尾阅读本文档,以获得对我们最喜爱的时间序列数据库的更全面了解。
database
field key
field set
field value
measurement
point
retention policy
series
tag key
tag set
tag value
timestamp
示例数据
下一节将参考下面列出的数据。 虽然数据是伪造的,但在InfluxDB中是一个很通用的场景。 数据展示了在2015年8月18日午夜至2015年8月18日上午6时12分在两个地点location
(地点1
和地点2
)显示两名科学家scientists
(langstroth
和perpetua
)计数的蝴蝶(butterflies
)和蜜蜂(honeybees
)数量。 假设数据存在名为my_database
的数据库中,而且存储策略是autogen
。
其中census是measurement
,butterflies和honeybees是field key
,location和scientist是tag key
。
讨论
现在您已经在InfluxDB中看到了一些示例数据,本节将详细分析这些数据。
接下来两个列叫作butterflies
和honeybees
,称为fields。fields由field key和field value组成。field key(butterflies
和honeybees
)都是字符串,他们存储元数据;field key butterflies
告诉我们蝴蝶的计数从12到7;field key honeybees
告诉我们蜜蜂的计数从23变到22。
field value就是你的数据,它们可以是字符串、浮点数、整数、布尔值,因为InfluxDB是时间序列数据库,所以field value总是和时间戳相关联。
在示例中,field value如下:
在上面的数据中,每组field key和field value的集合组成了field set
,在示例数据中,有八个field set
:
field是InfluxDB数据结构所必需的一部分——在InfluxDB中不能没有field。还要注意,field是没有索引的。如果使用field value作为过滤条件来查询,则必须扫描其他条件匹配后的所有值。因此,这些查询相对于tag上的查询(下文会介绍tag的查询)性能会低很多。 一般来说,字段不应包含常用来查询的元数据。
样本数据中的最后两列(location
和scientist
)就是tag。 tag由tag key和tag value组成。tag key和tag value都作为字符串存储,并记录在元数据中。示例数据中的tag key是location
和scientist
。 location
有两个tag value:1
和2
。scientist
还有两个tag value:langstroth
和perpetua
。
在上面的数据中,tag set是不同的每组tag key和tag value的集合,示例数据里有四个tag set:
tag不是必需的字段,但是在你的数据中使用tag总是大有裨益,因为不同于field, tag是索引起来的。这意味着对tag的查询更快,tag是存储常用元数据的最佳选择。
不同场景下的数据结构设计如果你说你的大部分的查询集中在字段
honeybees
和butterflies
上:因为field是没有索引的,在第一个查询里面InfluxDB会扫描所有的
butterflies
的值,第二个查询会扫描所有honeybees
的值。这样会使请求时间很长,特别在规模很大时。为了优化你的查询,你应该重新设计你的数据结果,把field(butterflies
和honeybees
)改为tag,而将tag(location
和scientist
)改为field。现在
butterflies
和honeybees
是tag了,当你再用上面的查询语句时,就不会扫描所有的值了,这也意味着查询更快了。
measurement作为tag,fields和time列的容器,measurement的名字是存储在相关fields数据的描述。 measurement的名字是字符串,对于一些SQL用户,measurement在概念上类似于表。样本数据中唯一的测量是census
。 名称census
告诉我们,fields值记录了butterflies
和honeybees
的数量,而不是不是它们的大小,方向或某种幸福指数。
注意:在单节点的实例下,Replication系数不管用。
在样本数据中,measurement census
中的所有内容都属于autogen
的retention policy。 InfluxDB自动创建该存储策略; 它具有无限的持续时间和复制因子设置为1。
现在你已经熟悉了measurement,tag set和retention policy,那么现在是讨论series的时候了。 在InfluxDB中,series是共同retention policy,measurement和tag set的集合。 以上数据由四个series组成:
任意series编号
retention policy
measurement
tag set
series 1
autogen
census
location = 1,scientist = langstroth
series 2
autogen
census
location = 2,scientist = langstroth
series 3
autogen
census
location = 1,scientist = perpetua
series 4
autogen
census
location = 2,scientist = perpetua
理解series对于设计数据schema以及对于处理InfluxDB里面的数据都是很有必要的。
最后,point就是具有相同timestamp的相同series的field集合。例如,这就是一个point:
例子里的series的retention policy为autogen
,measurement为census
,tag set为location = 1, scientist = perpetua
。point的timestamp为2015-08-18T00:00:00Z
。
数据库可以有多个users,retention policy,continuous和measurement。 InfluxDB是一个无模式数据库,意味着可以随时添加新的measurement,tag和field。 它旨在使时间序列数据的工作变得非常棒。
Last updated
Was this helpful?