为什么 Postgresql Explain Cost 在 Limit 和 Result 阶段低而在 Index Scan 阶段高

问题描述 投票:0回答:0

共享计划中的索引扫描成本非常高(cost=0.56..696715.50

But Limit (cost=0.56..224.59 很低

以此类推 result cost 结果 (cost=224.59..224.60 rows=1 width=8)

为什么会这样?

现在我会认为这个查询是无害的,因为看起来最终成本仅在 CPU 上是 224.59,所以这不是 CPU 加载查询。

Postgresql 版本是 14.

explain analyze 
select min(ticketenti0_.created_at) as col_0_0_
  from ticket ticketenti0_
 where ticketenti0_.status=0
   and ticketenti0_.is_spam_ticket='false'
   and ticketenti0_.portal_id=0000
   and (ticketenti0_.group in (14 , 15 , 868 , 15 , 868 , 14));

查询计划

Result  (cost=224.59..224.60 rows=1 width=8) (actual time=9275.462..9275.464 rows=1 loops=1)   
InitPlan 1 (returns $0)     
->  Limit  (cost=0.56..224.59 rows=1 width=8) (actual time=9275.460..9275.461 rows=0 loops=1)           
      ->  Index Scan using idx_ticket_created_at_status_portal_id_group on ticket ticketenti0_  
           (cost=0.56..696715.50 rows=3110 width=8) (actual time=9275.458..9275.458 rows=0 loops=1)                 
          Index Cond: ((created_at IS NOT NULL) AND (status = 0) AND (portal_id = 0000))                 
          Filter: ((NOT is_spam_ticket) AND (group = ANY ('{14,15,868,15,868,14}'::bigint[])))                 
          Rows Removed by Filter: 148605

Planning Time: 0.926 ms
Execution Time: 9275.498 ms
postgresql query-optimization explain postgresql-performance
© www.soinside.com 2019 - 2024. All rights reserved.