前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2. InfluxDB关键概念

2. InfluxDB关键概念

作者头像
Devops海洋的渔夫
发布2022-01-14 17:33:49
3450
发布2022-01-14 17:33:49
举报
文章被收录于专栏:Devops专栏Devops专栏

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中

代码语言:javascript
复制
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是时间序列数据库,因此字段值始终与时间戳相关联。

示例数据中的字段值为:

代码语言:javascript
复制
12   23
1    30
11   28
3    28
2    11
1    10
8    23
7    22

在上面的数据中,字段键(field keys)和字段值(field values)对的集合构成了一个 字段集(field set)。以下是示例数据中的所有八个字段集:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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

执行如下:

代码语言:javascript
复制
> 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

代码语言:javascript
复制
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

操作如下:

代码语言:javascript
复制
> 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,属于索引,查询大规模数据的时候效率就会提升。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 海洋的渔夫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • InfluxDB前篇介绍
  • InfluxDB的关键概念
  • 样本数据 Sample data
  • 样本数据字段的含义说明
  • 查询条件中,索引很重要
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档