简体   繁体   English

如果我在 select 上使用星号,我的查询会更快。 为什么要这样做。 我正在使用 Postgres

[英]If I use asterisk on select, my query is faster. Why is it doing this. I'm using Postgres

I'm using Postgres 12.6.我正在使用 Postgres 12.6。

My First Query & Explain Result我的第一个查询和解释结果

        select
        *
        FROM   "coupon_coupon"
               LEFT OUTER JOIN "coupon_couponnumber"
                            ON ( "coupon_coupon"."id" =
                                 "coupon_couponnumber"."coupon_id" )
               LEFT OUTER JOIN "coupon_usercoupon"
                            ON ( "coupon_couponnumber"."id" =
                                 "coupon_usercoupon"."coupon_number_id" )

--- explain result
Hash Right Join  (cost=623.52..121842.44 rows=1282129 width=428)
  Hash Cond: (coupon_couponnumber.coupon_id = coupon_coupon.id)
  ->  Merge Left Join  (cost=0.85..117852.86 rows=1282129 width=173)
        Merge Cond: (coupon_couponnumber.id = coupon_usercoupon.coupon_number_id)
        ->  Index Scan using coupon_couponnumber_pkey on coupon_couponnumber  (cost=0.43..50788.71 rows=1282129 width=89)
        ->  Index Scan using coupon_usercoupon_coupon_number_id_idx on coupon_usercoupon  (cost=0.43..49083.82 rows=1185586 width=84)
  ->  Hash  (cost=488.96..488.96 rows=10696 width=255)
        ->  Seq Scan on coupon_coupon  (cost=0.00..488.96 rows=10696 width=255)

My Second Query & Explain Result我的第二次查询和解释结果

select
        coupon_coupon.id
        FROM   "coupon_coupon"
               LEFT OUTER JOIN "coupon_couponnumber"
                            ON ( "coupon_coupon"."id" =
                                 "coupon_couponnumber"."coupon_id" )
               LEFT OUTER JOIN "coupon_usercoupon"
                            ON ( "coupon_couponnumber"."id" =
                                 "coupon_usercoupon"."coupon_number_id" )
--- explain result
Hash Right Join  (cost=48091.56..93461.52 rows=1282129 width=4)
  Hash Cond: (coupon_couponnumber.coupon_id = coupon_coupon.id)
  ->  Hash Right Join  (cost=47468.90..89471.94 rows=1282129 width=4)
        Hash Cond: (coupon_usercoupon.coupon_number_id = coupon_couponnumber.id)
        ->  Seq Scan on coupon_usercoupon  (cost=0.00..24617.86 rows=1185586 width=4)
        ->  Hash  (cost=26433.29..26433.29 rows=1282129 width=8)
              ->  Seq Scan on coupon_couponnumber  (cost=0.00..26433.29 rows=1282129 width=8)
  ->  Hash  (cost=488.96..488.96 rows=10696 width=4)
        ->  Seq Scan on coupon_coupon  (cost=0.00..488.96 rows=10696 width=4)

I found column using asterisk on first query, and the second using specific column.我在第一个查询中找到使用星号的列,第二个使用特定列。 I don't understand why the second query is slower than the first one.我不明白为什么第二个查询比第一个慢。

The 1st one using (SELECT *) with a MERGE LEFT JOIN on indexes, while the 2nd using (SELECT AColumn) with HASH RIGHT JOIN .第一个使用(SELECT *)MERGE LEFT JOIN在索引上,而第二个使用(SELECT AColumn)HASH RIGHT JOIN

The cost of a hash join is greater than the merge. hash 连接的成本大于合并。 It's probably because PG underevaluate the cost of the hash strategy... This is a classical problem when the "planer" (PG optimizer) does not take the lenght of the rows returned properly as a count in the total query cost.这可能是因为 PG 低估了 hash 策略的成本......这是一个经典问题,当“planer”(PG 优化器)没有将正确返回的行的长度计入总查询成本时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM