PostgreSQL 中未使用的索引

Unused index in PostgreSQL

我现在正在学习 PostgreSQL 中的索引。我开始尝试创建索引并分析它将如何影响执行时间。我用这样的列创建了一些 table:

另外,我用数据填充了它们。之后我创建了自定义索引:

create index events_organizer_id_index on events(organizer_ID);

并执行此命令(事件 table 包含 148 行):

explain analyse select * from events where events.organizer_ID = 4;

我很惊讶搜索是在没有我的索引的情况下执行的,我得到了这个结果:

据我所知,如果我的索引被用于搜索,就会出现类似“事件索引扫描”的文本。 那么,有人可以解释或提供网站参考吗,如何有效地使用索引以及我应该在哪里使用它们来查看差异?

从“过滤器删除的行:125”中我看到事件中的行太少 table。只需添加几千行,再试一次

来自 docs

Use real data for experimentation. Using test data for setting up indexes will tell you what indexes you need for the test data, but that is all.

It is especially fatal to use very small test data sets. While selecting 1000 out of 100000 rows could be a candidate for an index, selecting 1 out of 100 rows will hardly be, because the 100 rows probably fit within a single disk page, and there is no plan that can beat sequentially fetching 1 disk page.

在大多数情况下,当数据库使用索引时,它只会获取行所在的地址。它包含数据 block_id 和偏移量,因为一个 4 或 8 Kb 的块中可能有很多行。

因此,数据库首先在索引中搜索块地址,然后在磁盘上查找块,读取它并解析您需要的行。

当行数太少时,它们可以放在几个数据块中,这使得数据库更容易、更快速地读取整个 table 而根本不使用索引。