在Postgres中,percentile_disc是否重用同一查询中其他percentile_disc的计算?

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

假设您有一些正在查询百分位数的表,两列使用相同的分组:

SELECT percentile_disc(0.25) WITHIN GROUP (ORDER BY my_col) as p25,
       percentile_disc(0.75) WITHIN GROUP (ORDER BY my_col) as p75 from my_table

Postgres 在计算第二列时是否重用为第一列计算的分组?

postgresql aggregate-functions
1个回答
0
投票

确实如此。您可以在

explain analyse verbose
中看到:无论添加多少个,都只会循环一次。这并不意味着您可以免费添加它们 - 执行时间将会增加。 db<>fiddle 的演示

explain analyse verbose
SELECT percentile_disc(0.25) WITHIN GROUP (ORDER BY my_col) as p25
      ,percentile_disc(0.75) WITHIN GROUP (ORDER BY my_col) as p75
      ,percentile_disc(0.3) WITHIN GROUP (ORDER BY my_col) as p
    ,percentile_disc(0.4) WITHIN GROUP (ORDER BY my_col)
    ,percentile_disc(0.5) WITHIN GROUP (ORDER BY my_col)
    ,percentile_disc(0.6) WITHIN GROUP (ORDER BY my_col)
    ,percentile_disc(0.7) WITHIN GROUP (ORDER BY my_col)
from my_table;
聚合(成本=8464.74..8464.75行=1宽度=56)(实际时间=343.493..343.495行=1循环=1)
输出:percentile_disc('0.25'::双精度)在组内(ORDER BY my_col)、percentile_disc('0.75'::双精度)在组内(ORDER BY my_col)、percentile_disc('0.3'::双精度)在组内组(ORDER BY my_col),percentile_disc('0.4'::双精度)组内(ORDER BY my_col),percentile_disc('0.5'::双精度)组内(ORDER BY my_col),percentile_disc('0.6'::双精度)组内(ORDER BY my_col),percentile_disc('0.7'::双精度)组内(ORDER BY my_col)
-> public.my_table 上的顺序扫描(成本=0.00..7214.38行=500138宽度=8)(实际时间=0.016..39.669行=500000循环=1
输出:my_col
规划时间:0.122毫秒
执行时间:344.358 ms
© www.soinside.com 2019 - 2024. All rights reserved.