InfluxDB关键概念

作者: Devops海洋的渔夫 | 来源:发表于2019-01-06 11:03 被阅读0次

    仅供学习,转载请注明出处

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

    关注微信公众号,回复【资料】、Python、PHP、JAVA、web,则可获得Python、PHP、JAVA、前端等视频资料。

    相关文章

      网友评论

        本文标题:InfluxDB关键概念

        本文链接:https://www.haomeiwen.com/subject/nhejrqtx.html