我正在使用SSMS 2016并尝试在JOIN和Where条件中使用的两列上创建Filtered nonclustered index。过滤索引时是否允许在WHERE子句中使用子查询。以下是相应的示例:
CREATE NONCLUSTERED INDEX [ncIX_MyId_&_EmpId_on_MyDB_Mytbl_optimized]
ON MyDB.dbo.[Mytbl_Optimized] ([MyId], [EmpId])
WHERE [MyId] IN
(SELECT DISTINCT Mytbl.[EmpId] FROM MyDB.dbo.Mytbl_Optimized AS t1 (NOLOCK)
LEFT OUTER JOIN AnotherDB.dbo.Another-tbl AS t2 WITH (NOLOCK)
ON t1.EmpId = t2.EmpId)
--The SSMS compiler says incorrect syntax near 'IN'...
但是当我将子查询替换为显式值时,如:
WHERE [MyId] IN (1,2) -- it works fine.
我的目标是提高未编制索引的视图的性能(使此视图索引不是一个选项,因为它每5分钟更新一次)。在该视图中,上面的select语句用作字段结果集:
CREATE VIEW
AS
col1,
Col2,
(SELECT DISTINCT Mytbl.[EmpId] FROM MyDB.dbo.Mytbl_Optimized AS t1 (NOLOCK)
LEFT OUTER JOIN AnotherDB.dbo.Another-tbl AS t2 WITH (NOLOCK)
ON t1.EmpId = t2.EmpId
WHERE AnotherDB.dbo.Table3.MyId = t1.MyId
FOR XML PATH('')) AS MyConcatenatedID
FROM AnotherDB.dbo.Table3
注意:tbl_Optimized表中的记录总数为460,000 表中唯一的EmpId值总数为26
答案 0 :(得分:0)
除非我误读this article,否则过滤后的索引无法帮助您查看。
另外,我不相信您可以使用子查询来定义索引中包含的内容。根据我的研究和测试,IN子句中的列表必须是明确的。