使用变量可以增加扫描和逻辑读取

时间:2011-08-11 18:03:09

标签: sql-server-2005 tsql

我无法理解这一点。我有一个查询,如果我在查询中硬编码一个EntityType的数字,这就是IO统计数据

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 'FranchiseAgreement'. Scan count 1, logical reads 157, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Address'. Scan count 1, logical reads 5485, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'County'. Scan count 1, logical reads 126, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'State'. Scan count 0, logical reads 505, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Country'. Scan count 0, logical reads 488, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'City'. Scan count 0, logical reads 630, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ARTaxMasterfile'. Scan count 4, logical reads 36, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ELMasterfile'. Scan count 1, logical reads 205, 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 'ELMasterfile'. Scan count 1, logical reads 205, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
***Table 'FranchiseAgreement'. Scan count 539932, logical reads 1750032, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Address'. Scan count 1, logical reads 5485, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'County'. Scan count 0, logical reads 481, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'State'. Scan count 0, logical reads 505, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Country'. Scan count 0, logical reads 488, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'City'. Scan count 0, logical reads 630, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ARTaxMasterfile'. Scan count 4, logical reads 36, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

DECLARE @BalanceIdEL SMALLINT
SET @BalanceIdEL = 14

PRINT 'Version 5'
SELECT Stores.*, Tax.ARTaxMasterFileId
FROM
(   -- Get The Addres of Each Lease
    SELECT 
        E.LeaseNum
    ,   E.LeaseDate
    ,   A.CountryCode
    ,   A.StateProvCode
    ,   A.County
    ,   A.City
    FROM 
        ELMasterFile E
    INNER JOIN
        FranchiseAgreement F
        ON  E.EntityId = F.FranchiseID
        AND E.EntityType = 7
    INNER JOIN
        Address A
        ON  F.FranchiseNum = A.EntityId
        AND F.SatelliteNum = A.SatelliteNum
        AND A.EntityType = 5 -- Store
        AND A.AddressType = 3 -- Store Address  
    )  Stores
INNER JOIN 
    -- Get the Taxes at each Level, Country, State, County and City
    (SELECT A.*, C.CountryCode, '' AS StateProvCode, '' AS CountyName, '' AS CityName
    FROM ARTaxMasterFile A
    INNER JOIN
        COMMON.dbo.Country C
        ON A.ARTaxLevelTypeId = C.CountryId 
    where BalanceId = @BalanceIdEL
    and ARTaxLevelType = 1
    UNION ALL
    SELECT A.*, S.CountryCode, S.StateProvCode AS StateProvCode, '' AS CountyName, '' AS CityName
    FROM ARTaxMasterFile A
    INNER JOIN
        COMMON.dbo.State S
        ON A.ARTaxLevelTypeId = S.StateId 
    where BalanceId = @BalanceIdEL
    and ARTaxLevelType = 2
    UNION ALL
    SELECT A.*, C.CountryCode, C.StateProvCode AS StateProvCode,  C.CountyName AS CountyName, '' AS CityName
    FROM ARTaxMasterFile A
    INNER JOIN
        COMMON.dbo.County C
        ON A.ARTaxLevelTypeId = C.CountyId 
    where BalanceId = @BalanceIdEL
    and ARTaxLevelType = 3
    UNION ALL
    SELECT A.*, Country.CountryCode, State.StateProvCode AS StateProvCode,  County.CountyName AS CountyName, City.CityName AS CityName
    FROM ARTaxMasterFile A
    INNER JOIN
        COMMON.dbo.City City
        ON A.ARTaxLevelTypeId = City.CityId 
    INNER JOIN
        COMMON.dbo.County County
        ON City.CountyId = County.CountyId
    INNER JOIN
        COMMON.dbo.State State
        ON City.StateId = State.StateId
    LEFT OUTER JOIN
        COMMON.dbo.Country Country
        ON City.CountryId = Country.CountryId       
    where BalanceId = @BalanceIdEL
    and ARTaxLevelType = 4) Tax
    ON  (Stores.CountryCode = Tax.CountryCode
        AND Tax.StateProvCode = ''
        AND Tax.CountyName = ''
        AND Tax.CityName = '')
    OR 
        (Stores.CountryCode = Tax.CountryCode
        AND Stores.StateProvCode = Tax.StateProvCode 
        AND Tax.CountyName = ''
        AND Tax.CityName = '')
    OR 
        (Stores.CountryCode = Tax.CountryCode
        AND Stores.StateProvCode = Tax.StateProvCode 
        AND Stores.County = Tax.CountyName 
        AND Tax.CityName = '')
    OR 
        (Stores.CountryCode = Tax.CountryCode
        AND Stores.StateProvCode = Tax.StateProvCode 
        AND Stores.County = Tax.CountyName 
        AND Stores.City =Tax.CityName)
WHERE Tax.StartDate <= GETDATE()
AND Tax.StartDate > Stores.LeaseDate 

最有趣的是该表上没有使用该参数 如果我取出Variable BalanceIdEL,并将其替换为14,则查询速度提高2倍。

1 个答案:

答案 0 :(得分:3)

我认为查询优化器没有意识到你的变量确实是一个常量,所以选择一个带有表扫描而不是索引查找的查询计划,因为它认为该值是未知的。查询计划说了什么?也许一个索引提示会强迫它做正确的事情。

由于这个原因,我大多停止使用变量作为常量 - 我现在使用带有注释的常量