为什么Postgres不使用索引?

Shp*_*ord 3 sql database postgresql indexing

我有一个名为整数列的表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)
Run Code Online (Sandbox Code Playgroud)

知道为什么会这样吗?

Den*_*rdy 8

因为:

Seq Scan on invoices  (...) (actual ... rows=118027 <— this
   Filter: (account_id = 1)
   Rows Removed by Filter: 51462                    <— vs this
 Total runtime: 39.917 ms
Run Code Online (Sandbox Code Playgroud)

你选择了这么多行,阅读整个表会更便宜.

相关的早期问题和答案从今天开始进一步阅读:

(另请参阅Craig关于第二个问题的更长答案,以获取有关索引细微差别的其他说明.)