为什么索引视图不能有 MAX() 聚合?

发布于 2024-08-18 17:33:49 字数 137 浏览 4 评论 0原文

我一直在尝试一些索引视图,印象深刻,但我几乎总是需要最大值或最小值,并且无法理解为什么它不适用于这些视图,任何人都可以解释为什么吗?

我知道他们是不允许的,我只是不明白为什么!允许计数等,为什么不允许 MIN/MAX,我正在寻找解释......

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 explain why?

I KNOW they are not allowed, I just can't understand why!!! Count etc. is allowed why not MIN/MAX, I'm looking for explanation...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月蹉跎了容颜 2024-08-25 17:33:50

不允许使用这些聚合,因为它们不能仅根据更改的值重新计算。

某些聚合(例如 COUNT_BIG()SUM())只需查看更改的数据即可重新计算。这些在索引视图中是允许的,因为如果基础值发生变化,可以直接计算该变化的影响。

其他聚合(例如 MIN()MAX())不能仅通过查看正在更改的数据来重新计算。如果删除当前的最大值或最小值,则必须在整个表中搜索并找到新的最大值或最小值。

同样的原则也适用于其他聚合,例如 AVG() 或标准变体聚合。 SQL 无法仅根据更改的值重新计算它们,而是需要重新扫描整个表以获取新值。

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.

爱你是孤单的心事 2024-08-25 17:33:50

索引视图不支持 MIN/MAX 等聚合函数。您必须在围绕视图的查询中执行 MIN/MAX 操作。

关于索引视图中允许和不允许的内容有完整的定义 此处 (SQL 2005)。

报价:

AVG、MAX、MIN、STDEV、STDEVP、VAR、
或 VARP 聚合函数。如果
AVG(表达式) 指定于
引用索引视图的查询,
优化器可以经常计算
如果视图选择所需的结果
列表包含 SUM(表达式) 和
COUNT_BIG(表达式)。例如,一个
索引视图 SELECT 列表不能
包含表达式 AVG(column1)。
如果视图 SELECT 列表包含
表达式 SUM(column1) 和
COUNT_BIG(column1),SQL Server 可以
计算查询的平均值
引用视图并指定
平均值(第 1 列)。

Aggregate functions like MIN/MAX aren't supported in indexed views. You have to do the MIN/MAX in the query surrounding the view.

There's a full definition on what is and isn't allowed within an indexed view here (SQL 2005).

Quote:

The AVG, MAX, MIN, STDEV, STDEVP, VAR,
or VARP aggregate functions. If
AVG(expression) is specified in
queries referencing the indexed view,
the optimizer can frequently calculate
the needed result if the view select
list contains SUM(expression) and
COUNT_BIG(expression). For example, an
indexed view SELECT list cannot
contain the expression AVG(column1).
If the view SELECT list contains the
expressions SUM(column1) and
COUNT_BIG(column1), SQL Server can
calculate the average for a query that
references the view and specifies
AVG(column1).

夜未央樱花落 2024-08-25 17:33:50

如果您只想在使用视图时查看排序的内容而不添加排序依据,我只需在其中添加一列并排序。

id = row_number() over (order by col1, col2) 

if you just want to see things ordered without adding a sort by when you use a view I just add a column with and order by in it.

id = row_number() over (order by col1, col2) 
生生漫 2024-08-25 17:33:50

除了 Remus 指定的原因外,支持 MIN 和 MAX 的实际需要较少。

与 COUNT() 或 SUM() 不同,MAX 和 MIN 的计算速度很快 - 只需一次查找即可完成设置 - 无需读取大量数据。

Besides the reasons specified by Remus, there is less practical need to support MIN and MAX.

Unlike COUNT() or SUM(), MAX and MIN are fast to calculate - you are all set after just one lookup - you don't need to read a lot of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文