【发布时间】:2021-11-08 19:08:10
【问题描述】:
此表开始用于计量表数据的短期存储,然后将被验证并添加到一些长期存储表中。
事实证明,自从我们保存了这些数据后,客户希望将其保留很长时间,而且它正在快速增长。
create table metering_meterreading
(
id bigserial not null. # Primary Key
created_at timestamp with time zone not null,
updated_at timestamp with time zone not null,
timestamp timestamp with time zone not null, # BTREE index
value numeric(15, 3) not null,
meter_device_id uuid not null, # FK to meter_device, BTREE index
series_id uuid not null # FK to series, BTREE index
organization_id uuid not null. # FK to org , BTREE index
);
我计划删除主键,因为 (org_id,meter_device_id,series_id,timestamp) 使它独一无二。它只是由我的 ORM (django) 添加的,当我们开始时我不在乎。
但由于我几乎总是想在组织、仪表设备和系列中进行过滤以获取一系列时间序列数据,我想知道在 (组织 ID、仪表设备 ID、系列 ID、时间戳)上设置多列索引是否更有效) 而不是单独的索引。
我在某处读到,如果我有一个范围,它应该是索引中最右边的。
对于时间序列数据,这仍然不是一个超级高效的表,因为它会变得很大,但我计划通过按范围分区来解决这个问题,或者甚至使用 Timescale。但在分区之前,我希望它尽可能高效地在其中查找数据。
我还在某处看到了一个示例,它使用单独的表格来识别指标:
create table metric
(
id
organization_id
meter_device_id
series_id
) UNIQE (organization_id, meter_device_id, series_id)
;
create table metering_meterreading
(
metric_id. bigserial, FK to metric, BTREE index
timestamp timestamp with time zone not null, # BTREE index
value numeric(15, 3) not null,
created_at timestamp with time zone not null,
updated_at timestamp with time zone not null,
);
但我不确定这是否真的比把它们都放在桌子上更好。由于现在涉及另一个表,因此可能会影响摄取率。
【问题讨论】:
标签: postgresql indexing