【问题标题】:SQL: Fast Cumulative Frequency Query (postgres)SQL:快速累积频率查询(postgres)
【发布时间】:2011-06-08 02:05:12
【问题描述】:

我希望从我们的数据库中获取累积频率数据。我创建了一个简单的临时表,其中包含我们看到的所有唯一状态更新计数,以及具有该状态更新数量的用户数量。

     Table "pg_temp_4.statuses_count_tmp"
     Column     |  Type   | Modifiers 
----------------+---------+-----------
 statuses_count | integer | 
 frequency      | bigint  | 
Indexes:
    "statuses_count_idx" UNIQUE, btree (statuses_count)

我目前的查询是:

select statuses_count, frequency/(select * from total_statuses)::float, (select sum(frequency)/(select * from total_statuses)::float AS percentage from statuses_count_tmp WHERE statuses_count <= SCT.statuses_count) AS cumulative_percent  FROM statuses_count_tmp AS SCT ORDER BY statuses_count DESC;

但这需要相当长的时间,而且查询的数量增长很快。因此,对于我拥有的约 50,000 行,我正在查看要读取的 50,000 个阶乘行。坐在这里看着查询逐渐消失,我希望有一个更好的解决方案,我还没有完成。

希望得到这样的东西:

0       0.26975161      0.26975161
1       0.15306534      0.42281695
2       0.05513516      0.47795211
3       0.03050646      0.50845857
4       0.02064444      0.52910301

【问题讨论】:

    标签: sql postgresql cumulative-frequency


    【解决方案1】:

    应该可以用窗口函数解决,假设你有 PostgreSQL 8.4 或更高版本。我猜total_statuses 是类似于select sum(frequency) from statuses_count_tmp 的视图或临时表?我在这里将其写为 CTE,这应该使它在语句期间只计算一次结果:

    with total_statuses as (select sum(frequency) from statuses_count_tmp)
    select statuses_count,
           frequency / (select * from total_statuses) as frequency,
           sum(frequency) over(order by statuses_count)
               / (select * from total_statuses) as cumulative_frequency
    from statuses_count_tmp
    

    如果没有 8.4 的窗口函数,最好的办法就是迭代地处理数据:

    create type cumulative_sum_type as ( statuses_count int, frequency numeric, cumulative_frequency numeric );
    create or replace function cumulative_sum() returns setof cumulative_sum_type strict stable language plpgsql as $$
    declare
      running_total bigint := 0;
      total bigint;
      data_in record;
      data_out cumulative_sum_type;
    begin
      select sum(frequency) into total from statuses_count_tmp;
      for data_in in select statuses_count, frequency from statuses_count_tmp order by statuses_count
      loop
        data_out.statuses_count := data_in.statuses_count;
        running_total := running_total + data_in.frequency;
        data_out.frequency = data_in.frequency::numeric / total;
        data_out.cumulative_frequency = running_total::numeric / total;
        return next data_out;
      end loop;
    end;
    $$;
    select * from cumulative_sum();
    

    【讨论】:

    • 啊,没有这样的运气。 8.3.9 并且没有真正希望在接下来的几天内更新它,但是一旦我们更新它,我会记住这个解决方案。
    • @Peck:我使用 plpgsql 函数添加了一个适用于 8.3 的解决方案。
    • 嗯。 frequency / (select * from total_statuses) 在我看来根本不合适。你怎么能把一个数字除以一个“集合”。不应该是frequency / (select count(*) from total_statuses)吗?
    • 假设with total_statuses as (select sum(frequency) from statuses_count_tmp),那么select * from total_statuses 可以被解释为一个标量,因为关系有一个带有单个字段的元组。
    • @a_horse_with_no_name:它只是一个具有单个值的临时表。不是套装。 @araqnid 在他的帖子中做了这个假设,我应该更清楚一点。
    猜你喜欢
    • 2010-09-15
    • 2012-10-16
    • 1970-01-01
    • 2021-05-13
    • 2020-07-24
    • 2020-01-30
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    相关资源
    最近更新 更多