文档文档

InfluxDB 模式设计

设计您的 模式 以获得更简单和更高性能的查询。遵循设计指南,使您的模式易于查询。了解这些指南如何带来更高性能的查询。

良好的模式设计可以防止高 series 基数性,从而提高查询性能。如果您注意到数据读取和写入速度减慢,或者想了解基数性如何影响性能,请参阅如何解决高基数性

为查询而设计

以下模式演示了易于查询的 measurementstag keysfield keys

measurementtag keytag keyfield keyfield key
airSensorsensorIdstationhumiditytemperature
waterQualitySensorsensorIdstationpHtemperature

airSensorwaterQualitySensor 模式说明了以下指南

以下数据点(格式为 Line Protocol)使用 airSensorwaterQualitySensor 模式

airSensor,sensorId=A0100,station=Harbor humidity=35.0658,temperature=21.667 1636729543000000000
waterQualitySensor,sensorId=W0101,station=Harbor pH=6.1,temperature=16.103 1472515200000000000

保持 measurements 和 keys 简洁

将数据存储在 tag valuesfield values 中,而不是 tag keysfield keysmeasurements 中。如果您将模式设计为将数据存储在 tag 和 field values 中,您的查询将更容易编写且效率更高。

此外,您将通过不在写入数据时创建 measurements 和 keys 来保持低基数性。要了解有关高 series 基数性对性能的影响的更多信息,请参阅如何解决高基数性

比较模式

比较以下以 Line Protocol 表示的有效模式。

推荐:以下模式在单独的 cropplotregion tags 中存储元数据。temp field 包含可变的数值数据。

Good Measurements schema - Data encoded in tags (recommended)
-------------
weather_sensor,crop=blueberries,plot=1,region=north temp=50.1 1472515200000000000
weather_sensor,crop=blueberries,plot=2,region=midwest temp=49.8 1472515200000000000

不推荐:以下模式将多个属性(cropplotregion)连接(blueberries.plot-1.north)在 measurement 中,类似于 Graphite 指标。

Bad Measurements schema - Data encoded in the measurement (not recommended)
-------------
blueberries.plot-1.north temp=50.1 1472515200000000000
blueberries.plot-2.midwest temp=49.8 1472515200000000000

不推荐:以下模式将多个属性(cropplotregion)连接(blueberries.plot-1.north)在 field key 中。

Bad Keys schema - Data encoded in field keys (not recommended)
-------------
weather_sensor blueberries.plot-1.north.temp=50.1 1472515200000000000
weather_sensor blueberries.plot-2.midwest.temp=49.8 1472515200000000000

比较查询

比较以下 良好 Measurements不良 Measurements 模式的查询。Flux 查询计算 north 区域中 blueberries 的平均 temp

易于查询良好 Measurements 数据可以轻松地按 region tag values 进行过滤,如以下示例所示。

// Query *Good Measurements*, data stored in separate tags (recommended)
from(bucket:"example-bucket")
    |> range(start:2016-08-30T00:00:00Z)
    |> filter(fn: (r) =>  r._measurement == "weather_sensor" and r.region == "north" and r._field == "temp")
    |> mean()

难以查询不良 Measurements 需要正则表达式从 measurement 中提取 plotregion,如以下示例所示。

// Query *Bad Measurements*, data encoded in the measurement (not recommended)
from(bucket:"example-bucket")
    |> range(start:2016-08-30T00:00:00Z)
    |> filter(fn: (r) =>  r._measurement =~ /\.north$/ and r._field == "temp")
    |> mean()

复杂的 measurements 使某些查询变得不可能。例如,使用 不良 Measurements 模式无法计算两个 plots 的平均温度。

保持 keys 简洁

除了保持您的 keys 不包含数据外,请遵循以下其他指南,使其更易于查询

避免 keys 中使用关键字和特殊字符

为了简化查询编写,请勿在 tag 和 field keys 中包含保留关键字或特殊字符。如果您在 keys 中使用 Flux 关键字,则需要将 keys 括在双引号中。如果您在 keys 中使用非字母数字字符,则需要在 Flux 中使用 方括号表示法

避免 tags 和 fields 的重复名称

避免在同一模式中为 tag keyfield key 使用相同的名称。如果您的 tag 和 field 具有相同的名称,则您的查询结果可能不可预测。

使用 tags 和 fields

Tag values 已索引,而 field values 未索引。这意味着查询 tags 比查询 fields 性能更高。您的查询应指导您在 tags 中存储什么以及在 fields 中存储什么。

对唯一和数值数据使用 fields

  • 将唯一或频繁变化的值存储为 field values。
  • 将数值存储为 field values。(Tags 仅存储字符串)。

使用 tags 提高查询性能

  • 如果值可以合理地索引,则将其存储为 tag values。
  • 如果值在 filter()group() 函数中使用,则将其存储为 tag values
  • 如果值在多个数据点之间共享,即 field 的元数据,则将其存储为 tag values。

由于 InfluxDB 索引 tags,查询引擎不需要扫描存储桶中的每个记录来定位 tag value。例如,考虑一个存储关于数千个用户的数据的存储桶。如果将 userId 存储在 field 中,则对用户 abcde 的查询需要 InfluxDB 扫描每一行中的 userId

from(bucket: "example-bucket")
    |> range(start: -7d)
    |> filter(fn: (r) => r._field == "userId" and r._value == "abcde")

为了更快地检索数据,请按 tag 过滤以减少扫描的行数。tag 应存储可以合理索引的数据。以下查询按 company tag 过滤,以减少为 userId 扫描的行数。

from(bucket: "example-bucket")
    |> range(start: -7d)
    |> filter(fn: (r) => r.company == "Acme")
    |> filter(fn: (r) => r._field == "userId" and r._value == "abcde")

保持 tags 简洁

每个数据属性使用一个 tag。如果您的源数据在一个参数中包含多个数据属性,请将每个属性拆分为其自己的 tag。当每个 tag 表示您的数据的一个属性(而不是多个连接的属性)时,您将减少在查询中对正则表达式的需求。没有正则表达式,您的查询将更易于编写且性能更高。

比较模式

比较以下以 Line Protocol 表示的有效模式。

推荐:以下模式将位置数据拆分为 plotregion tags。

Good Tags schema - Data encoded in multiple tags
-------------
weather_sensor,crop=blueberries,plot=1,region=north temp=50.1 1472515200000000000
weather_sensor,crop=blueberries,plot=2,region=midwest temp=49.8 1472515200000000000

不推荐:以下模式将多个属性(plotregion)连接在 location tag value (plot-1.north) 中。

Bad Tags schema - Multiple data encoded in a single tag
-------------
weather_sensor,crop=blueberries,location=plot-1.north temp=50.1 1472515200000000000
weather_sensor,crop=blueberries,location=plot-2.midwest temp=49.8 1472515200000000000

比较查询

比较 良好 Tags不良 Tags 模式的查询。Flux 查询计算 north 区域中 blueberries 的平均 temp

易于查询良好 Tags 数据可以轻松地按 region tag values 进行过滤,如以下示例所示。

// Query *Good Tags* schema, data encoded in multiple tags
from(bucket:"example-bucket")
    |> range(start:2016-08-30T00:00:00Z)
    |> filter(fn: (r) =>  r._measurement == "weather_sensor" and r.region == "north" and r._field == "temp")
    |> mean()

难以查询不良 Tags 需要正则表达式来解析复杂的 location values,如以下示例所示。

// Query *Bad Tags* schema, multiple data encoded in a single tag
from(bucket:"example-bucket")
    |> range(start:2016-08-30T00:00:00Z)
    |> filter(fn: (r) =>  r._measurement == "weather_sensor" and r.location =~ /\.north$/ and r._field == "temp")
    |> mean()

有关 InfluxDB 数据模型的概述,请观看以下视频


此页内容是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像现在一样使用它,而无需对代码进行任何更改。

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速、近实时数据引擎,可在本地磁盘或对象存储中收集和处理实时数据并持久化存储。InfluxDB 3 Enterprise 构建在 Core 的基础上,增加了高可用性、只读副本、增强的安全性和数据压缩,从而实现更快的查询和优化的存储。InfluxDB 3 Enterprise 的免费层级可供非商业家庭或业余爱好者使用。

有关更多信息,请查看