【发布时间】:2021-06-16 04:24:56
【问题描述】:
我有一个问题要问,以帮助我更好地理解索引。我们对此有何不同:
1
create index insertion_test_timestamp1_idx
on insertion_test (timestamp1);
create index insertion_test2_timestamp1_idx
on insertion_test (id13);
还有这个:
2
create index insertion_test_timestamp1_idx
on insertion_test (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,
select date_trunc('hour',timestamp1) as hour,max(id13) from timestamppsql where timestamp1 >='2020-01-01 00:05:00' and timestamp1<='2020-01-01 01:05:00' group by hour order by hour asc
我的版本是这样的:psql (PostgreSQL) 12.6 (Ubuntu 12.6-1.pgdg20.04+1)
【问题讨论】:
-
第一个创建两个索引。第二个创建一个包含两列的索引。它们非常不同。
-
"psql" 是默认客户端(有自己的版本)。 Postgres 版本在这里是相关的。 (
SELECT version())。最佳索引策略还将考虑您提到的hour列(与timestamp1相关的混淆)。披露确切的表定义(CREATE TABLE显示数据类型和约束的语句)、基数、典型读/写活动以及更多关于典型查询的信息。此处的说明:stackoverflow.com/tags/postgresql-performance/info
标签: sql postgresql indexing postgresql-performance