数据模型
MynaDB 使用与 InfluxDB Line Protocol 兼容的时序数据模型。 理解数据模型有助于您设计高效的 Schema 并编写最优的查询。
概述
MynaDB 中的时序数据由以下部分组成:
+------------------------------------------------------------------+
| 数据点 |
+------------------------------------------------------------------+
| |
| Measurement Tags Fields Timestamp |
| (表名) (索引元数据) (值) (时间) |
| |
| cpu host=server01 usage=75.5 1699900000|
| region=us-west temp=45 |
| |
+------------------------------------------------------------------+
| 组成部分 | 描述 | 是否索引 | 是否必需 |
|---|---|---|---|
| Measurement | 表名(例如 "cpu"、"memory") | 是 | 是 |
| Tags | 用于过滤的键值元数据 | 是 | 否 |
| Fields | 实际的指标值 | 否 | 是(至少一个) |
| Timestamp | 数据点的时间 | 是 | 否(默认为当前时间) |
Line Protocol 格式
数据使用 InfluxDB Line Protocol 写入:
<measurement>,<tag_key>=<tag_value>,... <field_key>=<field_value>,... [timestamp]
# Examples:
cpu,host=server01,region=us-west usage=75.5,temp=45i 1699900000000000000
memory,host=server01 used=16.5,total=32.0
disk,host=server01,mount=/data free=100.5,used=50.2
💡 语法规则
- Measurement 和 Tags 之间用逗号分隔(无空格)
- Tags 和 Fields 之间用空格分隔
- 多个 Tags/Fields 之间用逗号分隔
- Timestamp 是可选的,用空格分隔
Measurement
Measurement 类似于关系数据库中的表。它将相关的时序数据组织在一起。
命名规范
- 使用小写字母加下划线:
cpu_usage、network_io - 简洁但具有描述性
- 避免使用下划线以外的特殊字符
- 不要在名称中包含元数据(应使用 Tags)
# 好的 measurement 名称
cpu
memory
disk_io
http_requests
application_errors
# 不好的 measurement 名称(应使用 tags)
cpu_server01 # 应使用: cpu,host=server01
memory_us_west # 应使用: memory,region=us-west
Tags
Tags 是用于过滤和分组的索引键值对。它们以字符串形式存储, 非常适合用于需要查询的元数据。
何时使用 Tags
- 标识符:host、server_id、container_id
- 位置:region、datacenter、rack
- 分类:environment(prod/dev)、service、team
- 版本:app_version、os_version
# Tags 支持高效过滤
cpu,host=server01,region=us-west,env=prod usage=75.5
# 按 tags 查询
SELECT * FROM cpu WHERE host = 'server01'
SELECT * FROM cpu WHERE region = 'us-west' AND env = 'prod'
SELECT region, AVG(usage) FROM cpu GROUP BY region
⚠ 基数警告
避免使用高基数 Tags(唯一 ID、时间戳、随机值)。 高基数会增加存储开销并降低查询速度。 对于高基数数据,请使用 Fields。
Tag 最佳实践
| 推荐 | 不推荐 |
|---|---|
host=server01 |
request_id=abc123...(应使用 field) |
region=us-west |
timestamp=1699900000(应使用 timestamp) |
env=prod |
user_id=12345(高基数) |
Fields
Fields 包含实际的指标值。它们不被索引,因此按 Fields 过滤需要扫描数据, 但它们支持多种数据类型。
Field 类型
| 类型 | 后缀 | 示例 | 存储类型 |
|---|---|---|---|
| 浮点数 | (无) | usage=75.5 |
Float64 |
| 整数 | i |
count=42i |
Int64 |
| 无符号整数 | u |
bytes=1024u |
UInt64 |
| 字符串 | 引号 | status="ok" |
Utf8 |
| 布尔值 | t/f/true/false |
active=t |
Boolean |
# Different field types
system,host=server01 cpu=75.5,memory=16384i,active=t,status="healthy"
# Multiple fields per measurement
http_request,endpoint=/api/users latency=45.2,status_code=200i,success=t
何时使用 Fields
- 指标:cpu_usage、memory_bytes、request_latency
- 计数器:request_count、error_count
- 高基数数据:user_id、request_id
- 需要聚合的值:SUM、AVG、MIN、MAX
时间戳
时间戳表示数据点发生的时间。MynaDB 以纳秒为单位存储时间戳, 基于 Unix 纪元(1970 年 1 月 1 日)。
时间戳精度
| 精度 | 位数 | 示例 |
|---|---|---|
| 纳秒 | 19 | 1699900000000000000 |
| 微秒 | 16 | 1699900000000000 |
| 毫秒 | 13 | 1699900000000 |
| 秒 | 10 | 1699900000 |
# With explicit timestamp (nanoseconds)
cpu,host=server01 usage=75.5 1699900000000000000
# Without timestamp (uses server time)
cpu,host=server01 usage=75.5
# Query by time range
SELECT * FROM cpu
WHERE time > 1699900000000000000
AND time < 1699986400000000000
Schema 结构
MynaDB 以 Catalog.Schema.Table 层级组织数据:
Catalog(数据库)
└── mydb
├── Schema(服务)
│ ├── default
│ │ ├── Table(Measurement)
│ │ │ ├── cpu
│ │ │ ├── memory
│ │ │ └── disk
│ │ └── ...
│ ├── web
│ │ ├── cpu
│ │ └── requests
│ └── api
│ └── latency
└── _system
└── series_stats
Schema 策略
ServiceAsSchema(默认)
Service 标签成为 Schema 名称。
- Schema = service 标签值
- Table = measurement 名称
cpu,service=web → mydb.web.cpu
ServiceAsTable
Service 标签成为表名。
- Schema = "default"
- Table = service 标签值
cpu,service=web → mydb.default.web
Parquet 中的列类型
存储到 Parquet 时,数据被转换为 Arrow 类型:
| Line Protocol | Arrow 类型 | Parquet 类型 |
|---|---|---|
| Tag 值 | Utf8 | BYTE_ARRAY (STRING) |
| 浮点数 Fields | Float64 | DOUBLE |
| 整数 Fields | Int64 | INT64 |
| 布尔值 Fields | Boolean | BOOLEAN |
| Timestamp | Int64 | INT64 |
示例:完整数据模型
# Write data with proper structure
curl -X POST http://localhost:7000/write/mydb -d '
cpu,host=server01,region=us-west,env=prod usage=75.5,temp=45i,cores=8i
cpu,host=server02,region=us-east,env=prod usage=82.3,temp=50i,cores=16i
memory,host=server01,region=us-west used=16.5,total=32.0,cached=8.2
disk,host=server01,mount=/data free=100i,used=50i,iops=1500.5
'
# Query with proper filtering
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
region,
AVG(usage) as avg_cpu,
MAX(temp) as max_temp
FROM cpu
WHERE env = 'prod'
GROUP BY host, region
ORDER BY avg_cpu DESC
"