SQL Server计划缓存非常相似但不同的查询

时间:2014-12-11 13:11:05

标签: sql-server query-optimization query-cache

说我有这样的查询:

SELECT COUNT(*) FROM Investors
WHERE 1=1  
AND 
(
    investerName IN (SELECT investerName FROM Assets WHERE Assets.CompanyID=Investors.CompanyID AND Assets.CompanyID = @companyID )
    OR investerName IN 
        (
            SELECT investerName FROM InvestmentEnrollments WHERE InvestmentEnrollments .CompanyID=Investors.CompanyIDAND InvestmentEnrollments .state IN (3,4) AND InvestmentEnrollments.CompanyID = @companyID         
        )
) 
AND Investors.CompanyID=@companyID

和其他类似的查询如下:

SELECT COUNT(*) FROM Investors
WHERE 1=1  
AND 
(
    investerName IN (SELECT investerName FROM Assets WHERE Assets.CompanyID=Investors.companyID AND Assets.companyID = @companyID )
    OR investerName IN 
        (
            SELECT investerName FROM InvestmentEnrollments WHERE InvestmentEnrollments .companyID=Investors.companyID AND InvestmentEnrollments.state IN (3,4) AND InvestmentEnrollments.companyID = @companyID         
        )
) 
AND Investors.companyID=@companyID
AND Investors.userName = @someInvestorUserName

两个查询都足够相似,但是第二个查询在谓词中有额外的内容。那么SQL Server在这种情况下做了什么?它是否尝试将两者都放在同一个查询计划中?或者这些被认为是需要不同查询计划的两个不同查询?

1 个答案:

答案 0 :(得分:2)

查询不仅是不同的文本,它们在语义上是不同的。这些是不同的查询,需要不同的执行计划。没有任何共同点。