【问题标题】:Explain plan issue with group by clause in postgresql 8.4在 postgresql 8.4 中用 group by 子句解释计划问题
【发布时间】:2012-09-13 14:02:33
【问题描述】:

下面给出了详细的解释,与group by子句问题的解释计划有关。

表:web_categoryutfv1_24hr_ts_201209
列:“5mintime”、类别、点击次数、字节数、appid
行数:871
索引:“web_categoryutfv1_24hr_ts_201209_idx”btree(“5mintime”)

我正在运行以下查询:

select count(*) over () as t_totalcnt,
       max(hits) over () as t_maxhits,
       max(bytes) over () as t_maxbytes,
       *
from (
  select category,
         sum(hits) as hits,
         sum(bytes) as bytes
  from (
    select "5mintime",
           category,
           hits,
           bytes,
           appid,
           0 as tmpfield
    from web_categoryutfv1_24hr_ts_201209
    where "5mintime" >= '2012-09-12 00:00:00'
    and   "5mintime" < '2012-09-19 00:00:00'
  ) as tmp
  where "5mintime" >= '2012-09-12 00:00:00'
  and   "5mintime" <= '2012-09-18 23:59:59'
  and   appid in ('')
  group by category
  order by hits desc
) as foo limit 10

我从 t_totalcnt 变量得到总行返回 55。现在我分析了web_categoryutfv1_24hr_ts_201209 表并再次使用解释运行相同的查询

我得到以下执行计划:

-> 限制(成本=31.31..31.61 行=10 宽度=580) -> WindowAgg(成本=31.31..32.03 rows=24 宽度=580) -> 子查询扫描 foo (cost=31.31..31.61 ***rows=24*** width=580) -> 排序(成本=31.31..31.37 rows=24 width=31) 排序键:(sum(web_categoryutfv1_24hr_ts_201209.hits)) -> HashAggregate(成本=30.39..30.75 rows=24 width=31) -> web_categoryutfv1_24hr_ts_201209 上的 Seq 扫描(成本=0.00..27.60 行=373 宽度=31) 过滤器: (("5mintime" >= '2012-09-12 00:00:00'::timestamp without time zone) AND ("5mintime" = '2012-09-12 00:00:00'::timestamp without time zone) AND ("5mintime"

现在我得到了解释计划输出 HashAggregate (cost=30.39..30.75 rows=24 width=31),它说 rows=24 而实际上是总行回报应该是 55。当我从查询中删除 group by 子句时,我在解释计划输出和实际查询执行中得到了 373 行。

所以我想知道查询中的解释计划和分组子句是否存在问题?

【问题讨论】:

    标签: database postgresql explain postgresql-8.4 sql-execution-plan


    【解决方案1】:

    执行计划中显示的行是估计。只要它们在正确的大小范围内,一切都很好。如果它们完全关闭,通常意味着您的统计数据已过时。

    删除 group by 会改变预期的行数,因为 group by 会减少它们。

    所以我认为那里没有任何问题。

    您可以使用explain analyze 来比较执行计划中的实际值和实数。

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2011-01-03
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      相关资源
      最近更新 更多