postgresql - 为什么不使用索引?

标签 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 analyseSeq 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 行,您选择了所有列。如果它确实使用了索引,它就必须返回到表中以获取其余数据。对于这么小的表,扫描整个表会更快,尤其是因为看起来您无论如何都选择了协议(protocol)表的每一行和每一列。

关于postgresql - 为什么不使用索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56917508/

相关文章:

PostgreSQL 相当于 MySQL --force 标志

node.js - Typeorm连接到多个数据库

postgresql - PL/pgSQL函数随机选择一个id

java - CrudRepository delete 是否也删除了 Blob 字段的底层大对象

sql - plpgsql 中过程的返回类型

postgresql - 为什么 PostgreSQL 中的 xmin 和 xmax 值相同?

postgresql - libpq,带参数插入

python - 让flask-sqlalchemy使用db.create_all从模型创建表

postgresql - 混合 "Index-like"btree 结构 - PostgreSQL 可以做到这一点吗?

regex - 在 postgresql 中使用函数修改反向引用