DBAtlas · 数据库运维图谱

时间序列数据

Oracle

-- Range partitioning by date:
CREATE TABLE measurements (
  ts TIMESTAMP, sensor_id NUMBER, value NUMBER)
PARTITION BY RANGE (ts)
INTERVAL (NUMTODSINTERVAL(1,'DAY'))
( PARTITION p0 VALUES LESS THAN (TIMESTAMP '2026-01-01 00:00:00') );

在 Oracle 中,时间序列数据通常通过 RANGE(INTERVAL)分区管理;新分区自动创建,旧分区可轻松删除(分区裁剪可加速查询)。也会使用压缩(ILM/ADO)和用于聚合的物化视图。

PostgreSQL

-- Hypertable with the TimescaleDB extension:
CREATE EXTENSION IF NOT EXISTS timescaledb;
CREATE TABLE measurements (ts TIMESTAMPTZ, sensor_id INT, value DOUBLE PRECISION);
SELECT create_hypertable('measurements', 'ts');
-- Summarize with a continuous aggregate.

PostgreSQL 可通过原生分区(PARTITION BY RANGE)存储时间序列;TimescaleDB 扩展增加了 hypertable,提供基于时间的自动分块、压缩、保留策略和连续聚合。这是大型 IoT/指标负载的常用方案。

MySQL

-- RANGE partitioning by date:
CREATE TABLE measurements (
  ts DATETIME, sensor_id INT, value DOUBLE,
  PRIMARY KEY (ts, sensor_id))
PARTITION BY RANGE (TO_DAYS(ts)) (
  PARTITION p2026_01 VALUES LESS THAN (TO_DAYS('2026-02-01')),
  PARTITION pmax VALUES LESS THAN MAXVALUE);

MySQL 没有专门的时间序列引擎;RANGE 分区(配合 TO_DAYS/UNIX_TIMESTAMP)可快速删除旧数据并实现分区裁剪。对于高容量时间序列,通常优先选择外部方案(TimescaleDB、InfluxDB)或汇总表。

SQL Server

-- Partitioned table (partition function + scheme) or
-- system-versioned temporal table (history kept automatically):
CREATE TABLE dbo.measurements (
  id INT PRIMARY KEY, value DECIMAL(10,2),
  valid_from DATETIME2 GENERATED ALWAYS AS ROW START,
  valid_to DATETIME2 GENERATED ALWAYS AS ROW END,
  PERIOD FOR SYSTEM_TIME (valid_from, valid_to))
WITH (SYSTEM_VERSIONING = ON);

SQL Server 通过分区函数/分区方案对时间序列进行分区管理;这为跨越大日期范围的数据提供裁剪和快速归档。此外,系统版本控制的时态表(SYSTEM_VERSIONING)会自动保存行变更历史,并支持时间点查询(FOR SYSTEM_TIME)。

MongoDB

// Time-series collection (MongoDB 5.0+):
db.createCollection("measurements", {
  timeseries: {
    timeField: "ts",
    metaField: "sensor_id",
    granularity: "seconds"
  }
})

MongoDB 5.0 引入的时间序列集合会按时间和 meta 字段自动以压缩的类列式(分桶)格式存储数据;可减少磁盘和索引占用,并加速范围查询。timeField 是必需的;metaField(来源/标签)和 granularity 决定性能。

Redis

# Requires the RedisTimeSeries module (Redis Stack).
# Create a series with a retention period:
TS.CREATE sensor:1 RETENTION 86400000 LABELS type temperature
# Add a sample (with an automatic timestamp):
TS.ADD sensor:1 * 23.5

核心 Redis 没有时间序列类型;RedisTimeSeries 模块(Redis Stack)通过 TS.CREATE/TS.ADD 提供高速样本写入、自动保留(RETENTION)、降采样(TS.CREATERULE),以及通过 TS.RANGE/TS.MRANGE 进行基于标签的范围查询。没有该模块时用 Sorted Set 模拟。

Elasticsearch

# Data stream: automatically rolling indexes for time series
PUT _index_template/measurements-tpl
{ "index_patterns": ["measurements*"], "data_stream": {},
  "template": {
    "settings": { "index.mode": "time_series", "index.routing_path": ["sensor_id"] },
    "mappings": { "properties": {
      "@timestamp": { "type": "date" },
      "sensor_id": { "type": "keyword", "time_series_dimension": true },
      "temperature": { "type": "double", "time_series_metric": "gauge" } } } } }
PUT _data_stream/measurements

Elasticsearch 通过 data streams 管理时间序列数据;新的 backing index 会随时间自动创建(rollover),ILM 将旧数据在 hot/warm/cold 层之间迁移并删除。TSDS(index.mode: time_series)提供针对指标数据的存储优化;至少需要一个 time_series_dimension 字段、一个指向它的 index.routing_path 以及一个 @timestamp 字段(否则模板会被拒绝)。

在 DBAtlas 中打开 →