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,查詢速度快兩倍。
+1,我看這過了,除非他們,因爲他們改變是必要不使用@variables這樣。這有點像參數嗅探,但沒有變量是一個參數。 –