Skip to content
Advertisement

sum columns of a table, grouping by table partition

I have a table which holds daily metrics :

The table has more than 150 million rows. It is partitioned on DateID, per month (each partition holds rows which DateID from first of month until the first of the next month). Some of my partitions:

I would like to select sum(metric1), sum(metric2), per month.

What i am doing so far is to create a temp table which holds YYYYMM as MonthID and join with my table on substring(dateID,1,6) = MonthID and then select the sums grouping by MonthID. However this is too slow. I think It would be faster if somehow i could group by using directly the table’s partitions. Any way to do that? If not any way to improve the sum performance?

Advertisement

Answer

You can try two levels of aggregation:

The inner aggregation is explicitly on only the partitioning key.

I should note that I do not know if SQL Server optimizes group by on partitioned databases, when the partitioning key is an aggregation key. However, it might, so this is worth trying.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement