首页 > 解决方案 > 在 Timescaledb 中创建一个具有两列的索引的正确方法

问题描述

我使用名为 Timescaledb 的 porstgreSQL 扩展。我有一个名为 .的表timestampdb。我想问一下这是否是创建一个包含两列(timestamp1,id13)的索引的正确方法。我使用的一些查询如下所示:

select * from timestampdb where timestamp1 >='2020-01-01 00:05:00' and timestamp1<='2020-01-02 00:05:00' and id13>'5',
select date_trunc('hour',timestamp1) as hour,avg(id13) from timestampdb where timestamp1 >='2020-01-01 00:05:00' and timestamp1<='2020-01-02 00:05:00' group by hour order by hour ,
select date_trunc('hour',timestamp1) as hour,max(id13) from timestampdb where timestamp1<='2020-01-01 00:05:00' group by hour order by hour desc limit 5

创建表后,我这样做:

create_hypertable("timestampdb",timestamp1) and then CREATE INDEX ON timestampdb (timestamp1,id13)

这是正确的方法吗?这会创建一个包含两列的索引吗?还是在 timestamp1 中创建一个索引,在 (timestamp1,id13) 中创建一个索引

标签: indexingtimescaledb

解决方案


是的,这是正确的方法。提供的调用实际上将创建一个与这两列相结合的索引。您要确保主要索引列(即第一列)是时间列。它在您的代码中。这样 tsdb 查询仍然会按时间首先找到您的数据(这在大型数据集上非常重要)。您的查询也与该索引匹配:它们主要基于时间范围进行搜索。

您可能想通过执行 EXPLAIN ANALYZE 来检查 postgres 执行查询的方式;或者使用 pgadmin 并单击“解释”按钮。这样,您可以确保您的 Indizes 被命中以及 postgres 是否有足够的堆缓冲区来缓存 tsdb 表页面或需要从磁盘读取(这实际上慢了 1000 到 10000 倍)。

我总是觉得这些资源很有帮助:TSDB YT 频道:https : //youtube.com/c/TimescaleDB TSDB Slack 频道:https ://slack.timescale.com/


推荐阅读