一尘不染

为什么Postgres不使用索引?

sql

我有一张表,其中有一个名为的整数列account_id。我在该列上有一个索引。

但是似乎Postgres不想使用我的索引:

EXPLAIN ANALYZE SELECT "invoices".* FROM "invoices" WHERE "invoices"."account_id" = 1;

 Seq Scan on invoices  (cost=0.00..6504.61 rows=117654 width=186) (actual time=0.021..33.943 rows=118027 loops=1)
   Filter: (account_id = 1)
   Rows Removed by Filter: 51462
 Total runtime: 39.917 ms
(4 rows)

知道为什么会这样吗?


阅读 229

收藏
2021-03-10

共1个答案

一尘不染

因为:

Seq Scan on invoices  (...) (actual ... rows=118027 <鈥� this
   Filter: (account_id = 1)
   Rows Removed by Filter: 51462                    <鈥� vs this
 Total runtime: 39.917 ms

您选择的行太多了,以至于读取整个表都比较便宜。

2021-03-10