I have a table with approximately 800K rows against which I run a common query. For example:
select zip,route_number,city,hh_count from tblRoutes
where event_id='MS020730'
and market_code='AL-ALB'
and job_no='001'
What's causing me trouble is that SQL chooses to execute this query in different ways depending upon the value of these query parameters. In other words, for the above query, the server does an Index Seek, a Bookmark Lookup, and a Select. The query returns three rows in less than a second, and the cost shown in the query plan within Query Analyzer is quite low. No problem.
However, if I run the exact same query with a minor change, as follows:
select zip,route_number,city,hh_count from tblRoutes
where event_id='MS020730'
and market_code='AL-MOB'
and job_no='001'
it returns 9 rows in over 40 seconds! Looking at the query plan in Query Analyzer explains why, because it's doing a Table Scan followed by a negligible Select operation. The cost of that Table Scan operation is a couple of orders of magnitude greater than the Seek and Lookup performed before.
Why would it execute nearly the same query in two different ways, and how can I force it to operate only the first way?
select zip,route_number,city,hh_count from tblRoutes
where event_id='MS020730'
and market_code='AL-ALB'
and job_no='001'
What's causing me trouble is that SQL chooses to execute this query in different ways depending upon the value of these query parameters. In other words, for the above query, the server does an Index Seek, a Bookmark Lookup, and a Select. The query returns three rows in less than a second, and the cost shown in the query plan within Query Analyzer is quite low. No problem.
However, if I run the exact same query with a minor change, as follows:
select zip,route_number,city,hh_count from tblRoutes
where event_id='MS020730'
and market_code='AL-MOB'
and job_no='001'
it returns 9 rows in over 40 seconds! Looking at the query plan in Query Analyzer explains why, because it's doing a Table Scan followed by a negligible Select operation. The cost of that Table Scan operation is a couple of orders of magnitude greater than the Seek and Lookup performed before.
Why would it execute nearly the same query in two different ways, and how can I force it to operate only the first way?