我想创建以下索引视图:
CREATE VIEW [Cic].[vwMarker] WITH SCHEMABINDING AS Select SubId,marker.EquipmentID,marker.ReadTime,marker.CdsLotOpside,marker.CdsLotBackside,marker.CdteLotOpside,marker.CdTeLotBackside From dbo.Marker Where dbo.Marker.ReadTime >= Convert(dateTime,'10/5/2011',120) GO CREATE UNIQUE CLUSTERED INDEX IX_vwMarker_ReadTime_EquipmentID ON Cic.vwMarker (ReadTime,EquipmentID);
这很好用.但是,我真正想要做的是在此视图中仅包含两天或更新的行,从查询视图的当前日期/时间开始.我找不到这样做的方法,因为我不能在Where谓词中使用GetDate(),因为它是非确定性的.换句话说,我想做这样的事情,但不能:
Where dbo.Marker.ReadTime >= Convert(dateTime,DateAdd(dd,-2,GetDate()),120)
有没有解决的办法?
解决方法
AFAIK您不会绕过SCHEMABINDING要求的确定性函数.你总会收到错误
The function ‘getdate’ yields nondeterministic results. Use a deterministic system function,or modify the user-defined function to return deterministic results.
如果Marker只是一个表,我不确定索引视图是否会比基于(ReadTime,EquipmentID)基础表上具有相同聚簇索引的表的普通视图具有任何性能优势
但是,如果“Marker”本身是一个复合体,例如VIEW,或者如果您不想更改Marker表上的Clustered Index,那么您可能会考虑以下内容:
>创建没有ReadDate过滤器的模式绑定视图(vwMarker)
>在未过滤的视图上创建索引视图
>创建第二个非模式绑定视图vwMarkerRecent等,它在非确定性GetDate过滤器中添加.
即:像:
CREATE VIEW [Cic].[vwMarker] WITH SCHEMABINDING AS Select SubId,marker.CdTeLotBackside From dbo.Marker -- Add only Deterministic where filters here GO CREATE UNIQUE CLUSTERED INDEX IX_vwMarker ON Cic.vwMarker (ReadTime,EquipmentID) GO CREATE VIEW [Cic].[vwRecentMarker] -- Not Schema Bound AS Select vm.SubId,vm.EquipmentID,vm.ReadTime,vm.CdsLotOpside,vm.CdsLotBackside,vm.CdteLotOpside,vm.CdTeLotBackside From cic.vwMarker vm Where vm.ReadTime >= Convert(dateTime,120) GO