Why can't indexed views have a MAX() aggregate?

后端 未结 4 635
轮回少年
轮回少年 2020-12-10 00:14

I have been trying out a few index views and am impressed but I nearly always need a max or a min as well and can not understand why it doesn\'t work with these, can anyone

4条回答
  •  天命终不由人
    2020-12-10 01:06

    These aggregates are not allowed because they cannot be recomputed solely based on the changed values.

    Some aggregates, like COUNT_BIG() or SUM(), can be recomputed just by looking at the data that changed. These are allowed within an indexed view because, if an underlying value changes, the impact of that change can be directly calculated.

    Other aggregates, like MIN() and MAX(), cannot be recomputed just by looking at the data that is being changed. If you delete the value that is currently the max or min, then the new max or min has to be searched for and found in the entire table.

    The same principle applies to other aggregates, like AVG() or the standard variation aggregates. SQL cannot recompute them just from the values changed, but needs to re-scan the entire table to get the new value.

提交回复
热议问题