dbTalk Databases Forums  

Is Not Null and Query Optimization

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Is Not Null and Query Optimization in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
db55
 
Posts: n/a

Default Is Not Null and Query Optimization - 06-28-2007 , 11:22 AM






How does the phrase "Is Not Null" in the where clause effect the
effectiveness of a query?

If it is a determent to the effectiveness of the query, how do you
work around it?

Thanks,


Reply With Quote
  #2  
Old   
Roy Harvey
 
Posts: n/a

Default Re: Is Not Null and Query Optimization - 06-28-2007 , 11:42 AM






On Thu, 28 Jun 2007 16:22:12 -0000, db55 <chfran (AT) gmail (DOT) com> wrote:

Quote:
How does the phrase "Is Not Null" in the where clause effect the
effectiveness of a query?

If it is a determent to the effectiveness of the query, how do you
work around it?

Thanks,
It really isn't an issue in general, with is the only way your
question can be answered. If you provide a specific case a more
specific response might be possible.

If the requirement is to test that something is NOT NULL then it is
intrinsic to the effectiveness of the query - the query can't be
writen without it.

As far as the efficiency of the query, the optimizer will do the best
it can to take advantage of any appropriate indexes when choosing an
execution plan.

Roy Harvey
Beacon Falls, CT


Reply With Quote
  #3  
Old   
Gert-Jan Strik
 
Posts: n/a

Default Re: Is Not Null and Query Optimization - 06-28-2007 , 02:43 PM



Are you sure this is the question you want to ask?

The "Is Not Null" is treated as a requirement, and will force the
optimizer to choose a query plan that will eliminate all NULL rows.

If you want to know about the effects on efficiency, well, the use of
"Is Not Null" in and of itself does not negate the use of indexes. In
other words (and somewhat simplified): if most rows are NULL and a few
rows are NOT NULL, and you have an index on this column, then the
optimizer will most likely use the index.

HTH,
Gert-Jan


db55 wrote:
Quote:
How does the phrase "Is Not Null" in the where clause effect the
effectiveness of a query?

If it is a determent to the effectiveness of the query, how do you
work around it?

Thanks,

Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Is Not Null and Query Optimization - 06-28-2007 , 03:35 PM



db55 (chfran (AT) gmail (DOT) com) writes:
Quote:
How does the phrase "Is Not Null" in the where clause effect the
effectiveness of a query?
About the same as "col = 3" will.

That is, if you say

SELECT * FROM tbl WHERE col IS NOT NULL

and col has a non-clustered index, SQL Server will use that index if only
a few rows has non-NULL values, but will ignore it it, if many rows have.
Simply because a scan is more efficient in this case.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) 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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.