indexing - 在 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) 中创建一个索引
解决方案
是的,这是正确的方法。提供的调用实际上将创建一个与这两列相结合的索引。您要确保主要索引列(即第一列)是时间列。它在您的代码中。这样 tsdb 查询仍然会按时间首先找到您的数据(这在大型数据集上非常重要)。您的查询也与该索引匹配:它们主要基于时间范围进行搜索。
您可能想通过执行 EXPLAIN ANALYZE 来检查 postgres 执行查询的方式;或者使用 pgadmin 并单击“解释”按钮。这样,您可以确保您的 Indizes 被命中以及 postgres 是否有足够的堆缓冲区来缓存 tsdb 表页面或需要从磁盘读取(这实际上慢了 1000 到 10000 倍)。
我总是觉得这些资源很有帮助:TSDB YT 频道:https : //youtube.com/c/TimescaleDB TSDB Slack 频道:https ://slack.timescale.com/
推荐阅读
- image - 带有自动表 addImage 的 JSPDF
- python - Pytest:runtimeerror 线程“主线程”中没有当前事件循环
- email - 团队不显示通过 SendGrid 发送的电子邮件
- python - 模块“sib_api_v3_sdk”没有属性“SMTPApi”
- yaml - 如何在 github 操作中使用 terraform 解密 Iam_user 的 pgp 密钥
- excel - 使用 VBA 更改粘贴特殊设置
- python - Python/Pandas:如何从数据框的列中删除诸如 MR.、MS.、MISS 之类的称呼
- pdf - 签名后 PDF 损坏
- python - 如何在 Python 的整数列表中计算真值?
- ocaml - 如何满足包括多态集在内的模块类型