SuperAPI AI 工具集

timescaledb

通用与其它

查看项目

更新于 10-16 03:36

技能介绍

用途:在涉及时序数据、hypertable、连续聚合(continuous aggregates)或压缩时,按 TimescaleDB 2.24.0 最佳实践进行建表、压缩与查询设计。适用触发条件包括文件名含 hypertable、timeseries、metrics、events、logs、sensor、continuous_agg、compression,或代码中出现 create_hypertable、continuous aggregate、compress_chunk、add_compression_policy。

主要能力:

  1. 快速重压缩:2.24.0 对压缩块更新可直接重压缩,仅解压受影响段,适用于迟到数据修正与回填。用 job_stats 查 policy_recompression 任务。
  2. 连续聚合直压:CREATE MATERIALIZED VIEW ... WITH (timescaledb.continuous, timescaledb.compress = true),再用 add_compression_policy 直接对聚合加压,无需物化 hypertable。
  3. UUIDv7:PostgreSQL 18 下聚合可引用 UUIDv7 列,如 count(DISTINCT id)。
  4. 布隆过滤:对稀疏列设置 timescaledb.compress_bloomfilter = 'error_code, trace_id',加速稀有非空值精确匹配,不适合范围查询。

使用方式: 建 hypertable:SELECT create_hypertable('metrics','time', chunk_time_interval => INTERVAL '1 day', create_default_indexes => true); 可按数据量选块间隔(<1GB/天用 1 周,1–10GB/天用 1 天,10–100GB/天用 6 小时,>100GB/天用 1 小时)。主键应包含时间列,如 PRIMARY KEY (id, time)。 压缩:ALTER TABLE 设 timescaledb.compress、compress_segmentby、compress_orderby;手工 SELECT compress_chunk(c) FROM show_chunks(...);自动用 add_compression_policy。segmentby 选常过滤且基数适中的列,高基数列放 orderby。 连续聚合:CREATE MATERIALIZED VIEW ... WITH (timescaledb.continuous) ... WITH NO DATA;可建层级聚合;用 add_continuous_aggregate_policy 设刷新策略(含 start_offset、end_offset、schedule_interval),materialized_only=false 启用实时聚合,refresh_continuous_aggregate 强制刷新。 保留与分层:add_retention_policy、remove_retention_policy;典型为原始数据压缩 7 天、保留 90 天,小时聚合压缩 30 天、保留 1 年,天聚合长期保留。 查询:按时间范围过滤,使用 time_bucket 聚合、DISTINCT ON 取每设备最新值、从连续聚合查询;百分位用 percentile_agg/stats_agg 与 approx_percentile。压缩块不走普通索引,需依赖块排除、segmentby 与 orderby。 迁移:确保时间列 NOT NULL,create_hypertable(..., migrate_data => true),再逐步加压缩与策略;确认版本用 SELECT extversion FROM pg_extension WHERE extname='timescaledb'。

限制:资料仅覆盖上述能力与用法;具体平台支持、许可与价格未提供。布隆过滤仅利于精确匹配;压缩块上索引行为受限。实际使用需 PostgreSQL 与 TimescaleDB 版本兼容。