I can’t understand this. I have a query and if I hard code a number for a EntityType in the query, this is the IO statistics
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.
nothing too bad. Now all I am going to do is replace the hard coded number with a variable for readability
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
The funniest thing is the parameter is not used on that table
If I take out the Variable BalanceIdEL, and replace it with 14, the query is 2x faster.
I think the query optimizer doesn’t realize that your variable is really a constant, and so picks a query plan with a table scan rather than an index lookup, since it believes the value is unknown. What does the query plan say? Maybe an index hint would forcew it to do the right thing.
I have mostly stopped using variables as constants for this reason – I now use constants with comments