Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:55:00+00:00 2026-05-24T10:55:00+00:00

I can’t understand this. I have a query and if I hard code a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:55:01+00:00Added an answer on May 24, 2026 at 10:55 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone maybe tell me why this is not working? I have used echo
can any one tell me how can change this java code into objective c.is
I have a jquery bug and I've been looking for hours now, I can't
Can anyone suggest how to underline the title of a UIButton ? I have
Can I have a project that has some parts written in c and other
Can i get the source code for a WAMP stack installer somewhere? Any help
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());
Can't work out a way to make an array of buttons in android. This
Can you have submenus with the top level set to checkable in WPF? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.