SQL Server 索引视图错误

发布于 2024-12-16 20:10:49 字数 1051 浏览 2 评论 0原文

我意识到这是一个非常人为的示例,但我已将完整版本简化为以下内容,以演示问题:

CREATE VIEW model.Appointments_Partition1
WITH SCHEMABINDING AS
  SELECT CONVERT(varchar(15), AppointmentId) as Id, 
         ap.AppTypeId as AppointmentTypeId, 
         ap.Duration as DurationMinutes, 
         ap.AppointmentId as EncounterId, 
         COUNT_BIG(*) as __count_big
    FROM dbo.Appointments ap 
    JOIN dbo.PracticeCodeTable pct ON SUBSTRING(pct.Code, 1, 1) = ap.ScheduleStatus 
                                  AND pct.ReferenceType = 'AppointmentStatus' 
   WHERE ap.AppTime > 0
GROUP BY CONVERT(varchar(15), AppointmentId), ap.AppTypeId, ap.Duration, ap.AppointmentId

CREATE UNIQUE CLUSTERED INDEX [IX_Appointments_Partition1_Id]
ON model.Appointments_Partition1 ([Id]);

我得到:

消息 8668,级别 16,状态 0,第 12 行
无法在视图“PracticeRepository.model.Appointments_Partition1”上创建聚集索引“IX_Appointments_Partition1_Id”,因为视图的选择列表包含聚合函数或分组列结果的表达式。考虑从选择列表中删除聚合函数或分组列结果的表达式。

我包括 count_big...那么为什么分组有问题?...以及如何解决该错误?

I realize this is a very contrived example, but I've simplified the full version down to the following which demonstrates the problem:

CREATE VIEW model.Appointments_Partition1
WITH SCHEMABINDING AS
  SELECT CONVERT(varchar(15), AppointmentId) as Id, 
         ap.AppTypeId as AppointmentTypeId, 
         ap.Duration as DurationMinutes, 
         ap.AppointmentId as EncounterId, 
         COUNT_BIG(*) as __count_big
    FROM dbo.Appointments ap 
    JOIN dbo.PracticeCodeTable pct ON SUBSTRING(pct.Code, 1, 1) = ap.ScheduleStatus 
                                  AND pct.ReferenceType = 'AppointmentStatus' 
   WHERE ap.AppTime > 0
GROUP BY CONVERT(varchar(15), AppointmentId), ap.AppTypeId, ap.Duration, ap.AppointmentId

CREATE UNIQUE CLUSTERED INDEX [IX_Appointments_Partition1_Id]
ON model.Appointments_Partition1 ([Id]);

I get:

Msg 8668, Level 16, State 0, Line 12
Cannot create the clustered index 'IX_Appointments_Partition1_Id' on view 'PracticeRepository.model.Appointments_Partition1' because the select list of the view contains an expression on result of aggregate function or grouping column. Consider removing expression on result of aggregate function or grouping column from select list.

I'm including count_big...so why is the group by a problem?....and how can I resolve the error?

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

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

发布评论

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

评论(2

行雁书 2024-12-23 20:10:49

以下是相同的错误消息,其中应用了一些布尔逻辑:

无法在视图“...”上创建聚集索引“...”,因为
视图的选择列表包含分组列上的表达式
考虑从选择列表中删除分组列上的表达式。

您需要删除 CONVERT(varchar(15), AppointmentId) 中的 CONVERT

Here is the same error message with some boolean logic applied to it:

Cannot create the clustered index '...' on view '...' because the
select list of the view contains an expression on a grouping column.
Consider removing expression on a grouping column from the select list.

You need to remove the CONVERT in CONVERT(varchar(15), AppointmentId)

无风消散 2024-12-23 20:10:49

我在其中一个博客上找到这个原因,对我来说似乎很合理

不,您不能在具有聚合的视图上使用架构绑定。除非使用架构绑定,否则无法对视图建立索引。您也无法绑定使用外连接或左连接的索引。基本上,您只能绑定包含简单 select 语句的视图。

http://www.tek-tips.com/viewthread.cfm?qid=1401646

您可以浏览该博客,看看它是否完全符合您的场景。

http://technet.microsoft.com/en-us/library/cc917715.aspx

如果你想在视图上建立索引,那么你必须创建带有模式绑定的视图,在上面的链接中有详细的解释。浏览设计注意事项部分

I find this reason on one of the blogs, seems reasonable to me

No, you can't use schema binding on a view that has an aggregate. And you can't index a view unless you use schema binding. You also can't bind an index that uses outer or left joins. Basically, you can only bind a view that contains a simple select statement.

http://www.tek-tips.com/viewthread.cfm?qid=1401646

You can go through the blog and see if it exactly matched your scenario.

http://technet.microsoft.com/en-us/library/cc917715.aspx

If you want to build index on views, then you must create views with schema binding, in the above link it is explained in detail. Go through the section of Design Considerations

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