【问题标题】:Why isn't Postgres using the index?为什么 Postgres 不使用索引?
【发布时间】:2014-02-06 21:35:19
【问题描述】:

我有一个表,其中包含一个名为 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)

知道为什么会这样吗?

【问题讨论】:

    标签: sql database postgresql indexing


    【解决方案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
    

    您选择的行太多,读取整个表格的成本更低。

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

    (有关索引微妙之处的其他说明,另请参阅 Craig 在第二个中的较长答案。)

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多