postgresql - 为什么不使用索引?
问题描述
我有疑问:
EXPLAIN ANALYSE
SELECT * FROM "order_bt" o
LEFT JOIN agreement a ON a.id = o.agreement_id
两个表都有索引:
"order_idx_agreement_id" btree (agreement_id)
"agreement_pkey" PRIMARY KEY, btree (id)
但explain analyse
说Seq Scan on agreement a
。为什么?
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
Hash Left Join (cost=36.97..199.70 rows=3702 width=193) (actual time=0.961..8.422 rows=3702 loops=1)
Hash Cond: (o.agreement_id = a.id)
-> Seq Scan on order_bt o (cost=0.00..116.02 rows=3702 width=136) (actual time=0.025..3.566 rows=3702 loops=1)
-> Hash (cost=22.54..22.54 rows=1154 width=57) (actual time=0.912..0.912 rows=1154 loops=1)
Buckets: 2048 Batches: 1 Memory Usage: 104kB
-> Seq Scan on agreement a (cost=0.00..22.54 rows=1154 width=57) (actual time=0.019..0.397 rows=1154 loops=1)
Planning time: 0.785 ms
Execution time: 8.886 ms
(8 rows)
解决方案
只有约 1000 行,您正在选择所有列。如果它确实使用了索引,它就必须返回表来获取其余数据。对于这么小的表格,扫描整个表格会更快,特别是因为看起来您无论如何都在选择协议表格的每一行和每一列。
推荐阅读
- android - Vimeo 视频播放器 - 安卓
- node.js - Preact 到底是什么?我可以使用节点模块吗?
- google-apps-script - 突然出现 Google Apps 脚本错误?无法访问未定义的属性
- python - 英文到法文日期转换问题
- html - 仅 CSS 视差滚动停止与 IOS/PadOS13 一起使用?
- npm - 我应该在 webpack 中包含哪个 babel-* 包来支持 IE11
- asp.net-core - 使用asp.net core 3.0 grpc时如何配置keepalive?
- javascript - 如果在第一列中找不到给定的关键字,如何删除一行?
- c++ - CppCheck 规则不显示#define,注释
- c - 并排打印两个金字塔(ASCII 图形)