您好我想在视图上创建一个索引,但我的视图使用了一个联合。请参见示例
SELECT t_1.RiskID,
t_1.HRASnapshotId,
t_1.RiskDate,
t_1.RiskTypeDescription,
t_1.AgillicUserId,
t_1.SequenceNo,
t_1.RiskValue,
t_1.RiskLevelDescription,
t_1.OrganisationID
FROM dbo.BE_Risks_Sequenced AS t
INNER JOIN dbo.BE_Risks_Sequenced AS t_1 ON
t.RiskTypeDescription = t_1.RiskTypeDescription
AND t.AgillicUserId = t_1.AgillicUserId
AND t.SequenceNo = t_1.SequenceNo + 1
WHERE (t.RiskValue = 1)
AND (t.RiskLevelDescription NOT LIKE t_1.RiskLevelDescription)
UNION ALL
SELECT RiskID, HRASnapshotId, RiskDate, RiskTypeDescription, AgillicUserId,
SequenceNo, RiskValue, RiskLevelDescription, OrganisationID
FROM dbo.BE_Risks_Sequenced
WHERE (SequenceNo = 1) AND (RiskValue > 1)
ORDER BY riskid asc
答案 0 :(得分:0)
你无法愚弄服务器。对索引视图的限制将阻止UNION,无论你怎么说它。您还可以在第一个查询中自我加入,这将成为另一个障碍。
两种可行的替代方案:
1)将查询输出的副本存储在非规范化表中,并使用触发器对其进行同步。这是混乱和资源效率低下的,但它可以很好地用于报告/分析。
2)创建一些可以索引的更简单的中间视图,并通过一个更复杂的无索引视图访问它们,该视图具有自连接和联盟。如有必要,请使用WITH(NOEXPAND)等表格提示。