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


本文分享自微信公众号 - DevOps社群(DevOpsCommunity)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

优秀的个人博客,低调大师

微信关注我们

原文链接:https://my.oschina.net/u/4011572/blog/4549661

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。