2011-08-11 50 views
1

我無法理解這一點。我有一個查詢,如果我在查詢中硬編碼一個實體類型的數字,這是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 

最有趣的是參數沒有在該表 使用。如果我拿出變量BalanceIdEL,而代之以14,查詢速度快兩倍。

回答

3

我認爲查詢優化器沒有意識到你的變量確實是一個常量,所以用表掃描而不是索引查找選擇查詢計劃,因爲它認爲值是未知的。查詢計劃說什麼?也許索引提示會迫使它做正確的事情。

使用變量作爲這個原因常數我大多停止了 - 我現在使用常量與評論

+0

+1,我看這過了,除非他們,因爲他們改變是必要不使用@variables這樣。這有點像參數嗅探,但沒有變量是一個參數。 –