postgresql - 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)
这样做的正确方法是什么?
解决方案
您可以创建没有时间维度列的索引,因为您不需要它是唯一的。如果索引包含UNIQUE
or is ,则需要将时间维度列包含到索引中PRIMARY KEY
,因为 TimescaleDB 将超表分区为时间维度列上的块,这timestamp1
在问题中。如果分区键除了时间之外还包括空间维度列,那么它们也需要包括在内。
因此,在您的情况下,迁移到超表后,以下内容就足够了:
create index on timestampdb(id13);
该问题包含两个查询,它们都不需要索引id13
。id13
如果您期望与问题不同的查询,则创建索引将很有价值,其中将包含条件或加入id13
列。
推荐阅读
- c++ - 从 GitHub 安装 R 包:当 R 包位于子目录中且 C++ 代码位于根目录时发生变化
- java - JPA更新时数据库中not null的值如何解决?
- swift - Alamofire 5,如何停止所有请求,刷新访问令牌并重新运行所有停止的请求?
- reactjs - 如何仅在渲染时在 React 组件中导入 SCSS 文件?
- python - 将嵌套字典和列表转换为 JSON 的问题
- javascript - 用玩笑模拟条件 window.open 函数调用
- python - 过滤 Pandas DataFrame 中的特定值
- angular - 将 *ngFor 动态添加到 HTML 元素
- python-3.x - 使用 tf-coreml 将 Tensorflow 转换为 CoreML
- java - Spring boot 2 embed tomcat 9.0.26 无法加载jks文件流关闭