473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Why optimizer is not smart? Is dynamic SQL my only option...

declare @ContactId as integer

set @ContactId = 5

select *

from Person.Contact

where ContactId = @ContactId

OR @ContactId = -1

If you run this in SQL 2005 on the AdventureWorks database,

why the logical reads is 561

Table 'Contact'. Scan count 1, logical reads 56

and not 2 when you run without the second OR condition:

declare @ContactId as integer

set @ContactId = 5

select *

from Person.Contact

where ContactId = @ContactId

How can i use the same SP and either get one record returned

by passing the ID of the field, or pass a dummy parameter like

-1 in order to get ALL the records returned.

In this case even when i pass a parameter like ContactID = 5

there is still a table scan (clustered index scan in this case)

happening for the other OR condition.

There's no method to tell SQL to start checking the first condition

whether or not it is true then if it is false then check the second OR

conditon. On the same topic does this mean all OR conditions are

ALWAYS verified regardless if one of them has already been determined

to be True?

Thank you


Feb 8 '07 #1
2 1351
serge (se****@nospam.ehmail.com) writes:
declare @ContactId as integer
set @ContactId = 5
select *
from Person.Contact
where ContactId = @ContactId
OR @ContactId = -1

If you run this in SQL 2005 on the AdventureWorks database,
why the logical reads is 561
Table 'Contact'. Scan count 1, logical reads 56

and not 2 when you run without the second OR condition:
Because the optimizer computes the plan, it does not know what the value of
@ContactId will be when the statement is executed.

I have a whole article on my web site on this topic,
http://www.sommarskog.se/dyn-search.html.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 8 '07 #2
To add on to Erland's response, OPTION (RECOMPILE) is a quick way to get a
better plan but it's not optimal in this case because there are 2 mutually
exclusive results in the same query. IMHO, the best approach in this case
is to formulate the query differently using an IF or UNION ALL.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"serge" <se****@nospam.ehmail.comwrote in message
news:oA*********************@wagner.videotron.net. ..
declare @ContactId as integer

set @ContactId = 5

select *

from Person.Contact

where ContactId = @ContactId

OR @ContactId = -1

If you run this in SQL 2005 on the AdventureWorks database,

why the logical reads is 561

Table 'Contact'. Scan count 1, logical reads 56

and not 2 when you run without the second OR condition:

declare @ContactId as integer

set @ContactId = 5

select *

from Person.Contact

where ContactId = @ContactId

How can i use the same SP and either get one record returned

by passing the ID of the field, or pass a dummy parameter like

-1 in order to get ALL the records returned.

In this case even when i pass a parameter like ContactID = 5

there is still a table scan (clustered index scan in this case)

happening for the other OR condition.

There's no method to tell SQL to start checking the first condition

whether or not it is true then if it is false then check the second OR

conditon. On the same topic does this mean all OR conditions are

ALWAYS verified regardless if one of them has already been determined

to be True?

Thank you

Feb 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
9
by: Andrea | last post by:
Hi, I've read the former postings but was not able to solve my problem: I have a Summary Table (or MQT as you like it) and the query optimizer does not seem to use the summary table. I run...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
5
by: Kevin | last post by:
Using a base table, a MQT table was created. With optimization - when querying the base table with calcuation that are already completed in the MQT - I would assume the optimizer would use the MQT...
0
by: robert | last post by:
I'm looking for an efficient optimizer on a noisy high-dimensional and costly function. My own GA hack seems to be somewhat stiff and I find me trying too much around with different cooling speeds...
3
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 Sorry if these are newbie questions. Optimizer stuff is black magic to me. For both of these, assume stats are current and an even distribution of data....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.