加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 为什么Postgres不使用索引?

发布时间:2020-12-12 16:22:06 所属栏目:MsSql教程 来源:网络整理
导读:我有一个名为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=18
我有一个名为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)

知道为什么会这样吗?

解决方法

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

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

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

> Why doesn’t Postgresql use index for IN query?
> Postgres using wrong index when querying a view of indexed expressions?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读