SQL Server 2008:自定义聚合函数和索引视图

发布于 2024-11-29 14:52:59 字数 886 浏览 5 评论 0原文

我在使用自定义 CLR 聚合函数的视图上创建索引时遇到问题。

我没有看到任何方法将聚合函数标记为确定性函数或具有模式绑定。

我正在像这样创建我的函数:

CREATE ASSEMBLY StringUtil
AUTHORIZATION dbo
FROM 'C:\StringUtil.dll'
WITH PERMISSION_SET = UNSAFE
GO

CREATE AGGREGATE SUMSTRING (@input nvarchar(200)) 
RETURNS nvarchar(max) WITH SCHEMABINDING 
EXTERNAL NAME StringUtil.Concatenate

我的视图定义为:

CREATE VIEW RolledValues WITH SCHEMABINDING
AS
SELECT ID, SumString(ValueText) as Value FROM [dbo].[IDValue]
GROUP BY ID

当我尝试在该视图上创建索引时出现问题:

CREATE UNIQUE CLUSTERED INDEX IDX_RollValues_ID_VALUE on RolledValues (ID)

Error:  Cannot create index on view "dbo.RolledValues" because it uses aggregate
"dbo.SumString". Consider eliminating the aggregate, not indexing the view, or 
using alternate aggregates.

那么是否可以在索引视图中使用自定义聚合函数?我找不到任何关于此的文档...

I'm having issues creating an index on a view that uses a custom CLR aggregate function.

I don't see any way to flag the aggregate function as deterministic or with schemabinding.

I'm creating my function like so:

CREATE ASSEMBLY StringUtil
AUTHORIZATION dbo
FROM 'C:\StringUtil.dll'
WITH PERMISSION_SET = UNSAFE
GO

CREATE AGGREGATE SUMSTRING (@input nvarchar(200)) 
RETURNS nvarchar(max) WITH SCHEMABINDING 
EXTERNAL NAME StringUtil.Concatenate

And my view is defined as:

CREATE VIEW RolledValues WITH SCHEMABINDING
AS
SELECT ID, SumString(ValueText) as Value FROM [dbo].[IDValue]
GROUP BY ID

The issue occurs when I try to create an index on that view:

CREATE UNIQUE CLUSTERED INDEX IDX_RollValues_ID_VALUE on RolledValues (ID)

Error:  Cannot create index on view "dbo.RolledValues" because it uses aggregate
"dbo.SumString". Consider eliminating the aggregate, not indexing the view, or 
using alternate aggregates.

So is it possible to use a custom aggregate function in an indexed view? I cannot find any documentation on this...

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

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

发布评论

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

评论(1

红尘作伴 2024-12-06 14:52:59

创建索引视图页面列出了许多限制:

视图中的 SELECT 语句不能包含以下 Transact-SQL 语法元素:

...

CLR 用户定义的聚合函数。

目前甚至没有规定将 CLR 聚合描述为确定性(至少,如果 API 想要保持一致的话)。 SqlFunctionAttribute 具有 IsDeterministic< /代码> 属性。 SqlUserDefinedAggregateAttribute 中不存在此类属性,


它确实有帮助如果您考虑为什么索引视图存在如此多的限制,就可以对事情进行推理。

关于聚合的解释非常简单 - 只允许使用具有 SQL Server 能够执行的属性的聚合(例如 SUMCOUNT_BIG)完全基于作为当前事务主题的行子集来调整值,或者从索引中添加或删除行。

例如,如果视图中有一行 ID=19、COUNT_BIG=5 和 SUM=96,并且事务删除 3 行 < code>ID 19,其 SUM 添加到 43,则可以将视图的该行更新为 COUNT_BIG=2 和 SUM=53。或者,如果事务删除了 ID=19 的 5 行,则会导致该行被删除。

请注意,无论哪种情况,我们都不必检查表的任何其他行来确定它们是否具有 ID=19。

那么 SQL Server 如何希望通过用户定义的聚合来实现类似的功能呢?用户定义聚合的当前接口没有您需要的那种支持(它还需要有一个类似接口的触发器)。

The page on Creating Indexed Views lists a number of restrictions:

The SELECT statement in the view cannot contain the following Transact-SQL syntax elements:

...

A CLR user-defined aggregate function.

There's not even provision, at the current time, to describe a CLR aggregate as Deterministic (at least, if the API was going to be consistent). The SqlFunctionAttribute has an IsDeterministic property. No such property exists in SqlUserDefinedAggregateAttribute


It does help to reason about things if you consider why so many restrictions exist on indexed views.

The ones on aggregates have a pretty simple explanation - you're only allowed to use aggregates (such as SUM and COUNT_BIG) that have the property that SQL Server will be able to adjust the values, or add or remove rows from the index, based purely on the subset of rows that are the subject of the current transaction.

E.g. if the view has a row with ID=19, COUNT_BIG=5, and SUM=96, and a transaction deletes 3 rows with ID 19, whose SUM adds to 43, then it can update that row of the view to be COUNT_BIG=2 and SUM=53. Alternatively, if the transaction had deleted 5 rows with ID=19, it would have caused the row to be removed.

Note that in either case, we don't have to examine any other rows of the table to determine if they have ID=19.

So how could SQL Server hope to achieve similar functionality with a user defined aggregate? The current interface for user defined aggregates doesn't have the sort of support you'd need (it would need to have a trigger like interface also).

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