我有下一个分区视图:vwSalesOutH。它由不同数据库中名为“SalesOutH”的几个表组成。
当我使用下一个查询时,它工作正常,搜索是通过一个表vwSalesOutH:
Kids_Iterator
统计IO:
.html
当我使用下一个查询时,在块“where”中使用了相同的日期,我遇到了问题 - 在分区视图中搜索所有表:
select vwSalesOutH.*
from vwSalesOutH
inner join DatesImport on vwSalesOutH.DBTypeId=DatesImport.DBTypeId and
vwSalesOutH.CustId=DatesImport.CustId
where vwSalesOutH.DBTypeId=2 and
vwSalesOutH.Date>='2017-01-01 00:00:00' and
vwSalesOutH.Date<='2017-01-25 00:00:00';
统计IO:
(6179 row(s) affected)
Table 'Workfile'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'SalesOutH'. Scan count 1, logical reads 452, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'DatesImport'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
您能否给我建议修复第二个查询?
答案 0 :(得分:1)
我遇到了同样的问题,问题似乎是除非分区值是作为常量提供的(未连接,不是变量),否则优化器无法识别要使用的分区而是扫描所有表格
即使使用Option(Recompile),对连接列的过滤也不会转到单表搜索。
使用带有Option(重新编译)的变量转到单表搜索。