【问题标题】:Covering index in Postgres 12Postgres 12 中的覆盖索引
【发布时间】:2021-05-21 09:19:05
【问题描述】:

我的表有 5500000 条测试记录(Postgres 12)。 当我在我的表上使用覆盖索引时,我有很大的不确定性。

所以,我创建索引:

create index concurrently idx_log_type
    on logs (log_type)
    include (log_body);

执行请求后:

explain (analyze)
select log_body
from logs where log_type = 1
limit 100;

我得到了结果:

Limit  (cost=0.43..5.05 rows=100 width=33) (actual time=0.118..0.138 rows=100 loops=1)
    - ->  Index Only Scan using idx_log_type on logs  (cost=0.43..125736.10 rows=2720667 width=33) (actual time=0.107..0.118 rows=100 loops=1)
        Index Cond: (log_type = 1)
        Heap Fetches: 0
Planning Time: 0.558 ms
Execution Time: 0.228 ms

乍一看,这很好,但成本范围非常大。 这是正常现象还是应该优化?

【问题讨论】:

    标签: postgresql optimization indexing


    【解决方案1】:

    这是EXPLAIN 的产物:它显示了完整索引扫描的成本,就像没有LIMIT 一样。但整个计划的定价是正确的。

    因此您可以忽略索引扫描的高成本上限。

    【讨论】:

    • 感谢您澄清这一点。我以前见过它,但不确定成本的上限。
    • @laurenz-albe,感谢您的解释性评论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2011-05-09
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多