Optimizing query with multiple sums?

删除回忆录丶 提交于 2021-02-10 15:46:26

问题


I have table products:

+----------+-----------+----------+---------+
|family_id |shopper_id |product_id|quantity |
+----------+-----------+----------+---------+
|A         |1          |Kit Kat   |10       |
|A         |1          |Kit Kat   |5        |
|A         |1          |Snickers  |9        |
|A         |2          |Kit Kat   |7        |
|B         |3          |Kit Kat   |2        |
+----------+---------- +----------+---------+

For each product, I want to calculate 2 totals:

  • total quantity per shopper
  • total quantity per family. Sum of total quantities for all shoppers in the same family.

The final table should look like:

+----------+----------+-------------------------+-----------------------+
|shopper_id|product_id|total_quantity_shopper   |total_quantity_family  |
+----------+----------+-------------------------+-----------------------+
|1         |Kit Kat   | 15                      | 22                    |
|1         |Snickers  | 9                       | 9                     |
|2         |Kit Kat   | 7                       | 22                    |
|3         |Kit Kat   | 2                       | 2                     |
+----------+----------+-------------------------|-----------------------|

This is my query:

SELECT
    distinct shopper_id,
    product_id,
    sum(quantity) OVER (PARTITION BY shopper_id, product_id) as total_quantity_shopper,
    sum(quantity) OVER (PARTITION BY family_id, product_id) as total_quantity_family
FROM
    products;

But looking at the query plan, it looks very inefficient (I think). How can I improve the query above?


回答1:


I think the family is a hierarchy for the shopper. So, I would suggest group by and window functions:

select family_id, shopper_id, product_id,
       sum(quantity) as total_quantity_shopper,
       sum(sum(quantity)) over (partition by family_id, product_id) as total_quantity_family
from products
group by family_id, shopper_id, product_id


来源:https://stackoverflow.com/questions/64687823/optimizing-query-with-multiple-sums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!