向视图添加索引时出错
Posted
技术标签:
【中文标题】向视图添加索引时出错【英文标题】:Error adding an index to a view 【发布时间】:2011-05-11 05:38:38 【问题描述】:我使用以下代码创建了一个视图
CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING
AS
SELECT dbo.day_dim.date_time AS Date,
dbo.order_dim.quantity AS Target_Acheived
FROM dbo.day_dim
JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id
JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id
JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time`
现在当我使用:
CREATE UNIQUE CLUSTERED INDEX two_weeks_performance_I ON two_weeks_performance (Date)
我收到一个错误:
无法创建索引,因为它的选择列表没有使用正确的 COUNT_BIG() 用法。考虑将 COUNT_BIG(*) 添加到选择中。
请帮我解决这个问题。
【问题讨论】:
【参考方案1】:该错误准确地告诉您必须做什么 - 将 COUNT_BIG(*) 添加到您的选择列表中。
来自Creating Indexed Views:
如果指定了 GROUP BY,则视图 选择列表必须包含 COUNT_BIG(*) 表达式和视图 定义不能指定 HAVING, 汇总、多维数据集或分组集。
CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING
AS
SELECT dbo.day_dim.date_time AS Date,
dbo.order_dim.quantity AS Target_Acheived,
COUNT_BIG(*) as Cnt
FROM dbo.day_dim
JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id
JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id
JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time
GO
CREATE UNIQUE CLUSTERED INDEX two_weeks_performance_I ON two_weeks_performance (Date)
【讨论】:
以上是关于向视图添加索引时出错的主要内容,如果未能解决你的问题,请参考以下文章
是否可以向临时表添加索引? create #t 和 declare @t 有啥区别
将补充标题视图添加到 UICollectionView 时出错
concurrentPerform UnsafeMutablePointer.deinitialize 向数组添加值时出错