首页 > 解决方案 > Timescaledb 上的索引

问题描述

我正在测试一些关于 Postgresql 扩展 Timescaledb 的查询。该表称为timestampdb,我对其运行一些查询,看起来像这样

select id13 from timestampdb where timestamp1 >='2010-01-01 00:05:00' and timestamp1<='2011-01-01 00:05:00',  
select avg(id13)::numeric(10,2) from timestasmpdb where timestamp1>='2015-01-01 00:05:00' and timestamp1<='2015-01-01 10:30:00'

当我创建一个超表时,我会这样做。 create hyper_table('timestampdb','timestamp1') 问题是现在我想在 id13 上创建一个索引。

我应该尝试这样的事情吗?:

create hyper_table('timestampdb','timestamp1') ,import data of the table and then  create index on timestampdb(id13)

或类似的东西:

create table timestampdb,then create hypertable('timestampdb',timestamp1') ,import the data and then CREATE INDEX ON timestampdb (timestamp1,id13)

这样做的正确方法是什么?

标签: postgresqlperformanceindexingtimescaledb

解决方案


您可以创建没有时间维度列的索引,因为您不需要它是唯一的。如果索引包含UNIQUEor is ,则需要将时间维度列包含到索引中PRIMARY KEY,因为 TimescaleDB 将超表分区为时间维度列上的块,这timestamp1在问题中。如果分区键除了时间之外还包括空间维度列,那么它们也需要包括在内。

因此,在您的情况下,迁移到超表后,以下内容就足够了:

create index on timestampdb(id13);

该问题包含两个查询,它们都不需要索引id13id13如果您期望与问题不同的查询,则创建索引将很有价值,其中将包含条件或加入id13列。


推荐阅读