【问题标题】:Correct way to create one index with two columns in Timescaledb在 Timescaledb 中创建一个具有两列的索引的正确方法
【发布时间】:2021-06-16 06:50:58
【问题描述】:

我使用名为 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) 中创建一个索引

【问题讨论】:

标签: indexing timescaledb


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 2020-06-28
    • 2016-11-18
    • 2016-08-03
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多