【发布时间】:2021-06-14 19:04:33
【问题描述】:
我有一个名为 timestamppsql 的表。我要在其上创建索引的列名为 timestamp1 和 id13。我要测试一堆查询,但其中大多数看起来像这样:
select date_trunc('day',timestamp1) as day,avg(id13) from timestamppsql where timestamp1 >='2010-01-01 00:05:00' and timestamp1<='2015-01-01 00:05:00' group by day order by day desc,
类似的东西
select id13 from timestamppsql where timestamp1 >='2010-01-01 00:05:00' and timestamp1<='2011-01-01 00:05:00',
select avg(id13)::numeric(10,2) from timestamppsql where timestamp1>='2015-01-01 00:05:00' and timestamp1<='2015-01-01 10:30:00'
我将创建一个索引:
create index project_index on timestamppsql(timestamp1,id13);
问题是索引的正确顺序是什么?timestamp1 first 还是 id13?
我知道按顺序排列的第一个索引(从左到右)应该是我们使用较多、重复值最少且列限制最多的索引。您对此有何看法?
【问题讨论】:
-
如有疑问,请评估两个版本。
-
@jarlh 我同意你的看法!我只是想谈谈以防我错过了什么
标签: sql postgresql indexing