文档文档

InfluxDB 模式设计

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

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

为查询而设计

以下模式演示了易于查询的 measurements标签键字段键

measurement标签键标签键字段键字段键
airSensorsensorIdstationhumiditytemperature
waterQualitySensorsensorIdstationpHtemperature

airSensorwaterQualitySensor 模式说明了以下指南

以下点(格式为行协议)使用 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

保持 measurement 和键的简洁

将数据存储在 标签值字段值 中,而不是在 标签键字段键measurements 中。 如果您设计模式以将数据存储在标签和字段值中,则您的查询将更易于编写且更高效。

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

比较模式

比较以下由行协议表示的有效模式。

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

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

不推荐:以下模式在 measurement 中连接(blueberries.plot-1.north)存储多个属性(cropplotregion),类似于 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

不推荐:以下模式在字段键中连接(blueberries.plot-1.north)存储多个属性(cropplotregion)。

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 区域中蓝莓的平均 temp

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

// 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 模式无法计算两个地块的平均温度。

保持键的简洁

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

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

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

避免标签和字段的重复名称

避免在同一模式中为 标签键字段键 使用相同的名称。 如果您有具有相同名称的标签和字段,则查询结果可能不可预测。

使用标签和字段

标签值 被索引,而 字段值 则未被索引。 这意味着查询标签比查询字段性能更高。 您的查询应指导您在标签中存储什么以及在字段中存储什么。

使用字段存储唯一和数值数据

  • 将唯一或频繁更改的值存储为字段值。
  • 将数值存储为字段值。(标签 仅存储字符串)。

使用标签提高查询性能

  • 如果值可以合理地被索引,则将值存储为标签值。
  • 如果 filter()group() 函数中使用这些值,则将值存储为 标签值
  • 如果值在多个数据点之间共享,即关于字段的元数据,则将值存储为标签值。

由于 InfluxDB 对标签进行索引,因此查询引擎无需扫描存储桶中的每个记录即可找到标签值。 例如,考虑一个存储数千个用户数据的存储桶。 如果将 userId 存储在 字段 中,则对用户 abcde 的查询需要 InfluxDB 扫描每一行中的 userId

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

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

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

保持标签的简洁

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

比较模式

比较以下由行协议表示的有效模式。

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

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

不推荐:以下模式在 location 标签值(plot-1.north)中连接存储多个属性(plotregion)。

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

比较查询

比较 良好标签不良标签 模式的查询。 Flux 查询计算 north 区域中蓝莓的平均 temp

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

// 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()

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

// 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 开源版本现已公开发布 Alpha 版

InfluxDB 3 开源版本现已可用于 Alpha 测试,根据 MIT 或 Apache 2 许可获得许可。

我们正在发布两个产品作为 Alpha 版本的一部分。

InfluxDB 3 Core 是我们的新开源产品。 它是用于时间序列和事件数据的最新数据引擎。 InfluxDB 3 Enterprise 是一个商业版本,它建立在 Core 的基础上,增加了历史查询功能、读取副本、高可用性、可扩展性和细粒度的安全性。

有关如何开始使用的更多信息,请查看