我有以下索引视图:
ALTER View [CIC].[vwCoaterC473Heat] WITH SCHEMABINDING
AS
Select
id as ID,
DATEADD(ms, -DATEPART(ms, read_time), read_time) as ReadTime,
equipment_id as EquipmentID,
...
...
From dbo.td_coater_c473_heat
Where read_time >= Convert(dateTime,'1/1/2012',120)
CREATE UNIQUE CLUSTERED INDEX [IX_vwCoaterC473Heat_ReadTime_EquipmentID_ID]
ON [CIC].[vwCoaterC473Heat]
(
[ReadTime] ASC,
[EquipmentID] ASC,
[ID] ASC
)
GO
我有以下查询引用索引视图:
Select
r.Coater,
r.ReadTime,
C473_left_A_actual_Temp,
C473_right_A_actual_Temp,
C473_left_B_actual_Temp,
C473_right_B_actual_Temp,
HD02A_Actual_Voltage,
HD02A_Actual_Current,
HD02B_Actual_Voltage,
HD02B_Actual_Current
From Cic.RandyTemp r
Inner Join Cic.vwCoaterC473Heat a
On a.EquipmentId = r.Coater And a.ReadTime = r.ReadTime
从中生成的查询计划如下所示:
我很好奇为什么SQL Server绕过索引视图中的持久数据,并查询视图所依据的基础表。
答案 0 :(得分:5)
您使用的是SQL Server标准版吗?如果是这样,您需要使用WITH (NOEXPAND)
提示。请尝试此版本,看看是否使用了索引视图。可能只是优化器决定访问基表中的索引更有效。
SELECT
r.Coater,
r.ReadTime,
C473_left_A_actual_Temp, -- why no alias prefixes from here down?
C473_right_A_actual_Temp,
C473_left_B_actual_Temp,
C473_right_B_actual_Temp,
HD02A_Actual_Voltage,
HD02A_Actual_Current,
HD02B_Actual_Voltage,
HD02B_Actual_Current
FROM Cic.RandyTemp AS r
INNER JOIN Cic.vwCoaterC473Heat AS a WITH (NOEXPAND)
ON a.EquipmentId = r.Coater
AND a.ReadTime = r.ReadTime;