如果将参数过滤器应用于内部查询,或者只是应用外部查询并让SQL查询引擎制定最佳执行计划?
我无法提出实际查询,因为它包含业务表/视图名称和列,以及相当多的连接,但实际上通用架构是......
Psuedo Sql ...
CREATE PROCEDURE usp_Blah_GetForDateRange
(
@DateFrom smalldatetime
, @DateTo smalldatetime
)
AS
BEGIN
SELECT
InnerQuery.MyField
FROM
(
SELECT
MyField
FROM
MyView
JOIN SomeTableLargeTable ON ....
JOIN AReferenceTable ON ...
/* Apply date restriction here on the inner query? */
WHERE
EffectiveDate BETWEEN @DateFrom AND @DateTo
) InnerQuery
JOIN
YetAnotherLargeTable ON ....
JOIN SomeMoreRefTables ON ....
/* Or here on the outer? */
WHERE
EffectiveDate BETWEEN @DateFrom AND @DateTo;
END
MyView,SomeTableLargeTable和YetAnotherLargeTable都有大约五六百万行。
感谢。
答案 0 :(得分:1)
根据经验,您越早排除查询中的数据越多越好。因此,如果没有实际运行查询,我会说内连接