472,958 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1344
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.