PostgreSQL 中的索引

Indexing in PostgreSQL

我有一个问题要问,以帮助我更好地理解 indexes.Do 我们认为这有什么不同:

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)

这取决于您的数据库模式和您正在执行的查询。只有在您的情况下同时实施两者才能为您提供关于哪个对您有好处的最佳答案。

PostgreSQL 能够在单个查询中使用多个索引,这是一个很棒的功能。

通过快速谷歌搜索,我找到了关于这个主题的一些很好的答案。请查看下方 link:

你的情况:

where timestamp1 = 'sometimestamp' 的查询使用 op 1 where id13 = someid13 使用 op 1

的查询

where timestamp1 = 'sometimestamp' and id13 = someid13 的查询,可以使用 op 2

where id13 = someid13 and timestamp1 = 'sometimestamp' 的查询,可以使用 op 1 或 op 2

为什么?因为索引 selection 涉及很多东西,基数、大小、统计数据、post select 操作等

记住,op 2 覆盖了 op 1 的第一个。