Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using COALESCE forces SQL to timeout

Posted on 2009-07-06
7
Medium Priority
?
867 Views
Last Modified: 2012-06-22
When I run

select * from company where comp_companyid = 2619

the query comes back immediately.  However, when I run

select * from company where coalesce(comp_companyid,0) = 2619

then SQL times out.   Why would it be doing this?
0
Comment
Question by:saturation
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24789847
it's possible, because the coalesce() function on the field makes that the index on the field cannot be used.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24789851
when u use a coalesce, sql has to perform another check operation to see whether companyid is null, if so set it as 0 ,

select * from company where comp_companyid = 2619
UNION ALL
select * from company where comp_companyid is null
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24789863
ie, I presume you run the query test-wise in your management studio, query window.
=> use the Query Menu => include actual execution plan

and rerun both queries.
you will get a nice graph of the 2 queries execution plans.
one with index lookup
the second one with full table (or full clustered index) scan
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:saturation
ID: 24789944
Also, I just noticed I'm getting the following error:

"SQL Execution Error"
Error Source: .NET SQLClient Data Provider
Error Message:  Lock request time out period exceeded.

I'm perplexed.  I have no problems querying any of the other tables in the database.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24789957
anyhow, note that:


select * from company where coalesce(comp_companyid,0) = 2619

will not produce any results different from

select * from company where comp_companyid = 2619

unless you wanted to find the null values:

select * from company where comp_companyid IS NULL


0
 

Author Comment

by:saturation
ID: 24789981
I do realize that; however, I'm troubleshooting an application that builds that query out and I don't have any control over the query...

Also, I just noticed that I can't even rebuild the indexes because of that error...Can individual tables be locked?   How do I release those locks if so?  
0
 
LVL 2

Accepted Solution

by:
corptech earned 2000 total points
ID: 24790071
I always use the IsNull (fieldname, 0).  There are arguments for and against using IsNull or Coalesce.  I've read of some performance issues with using coalesce.  Mostly with subqueries.  So, you could try the IsNull function and see if you get better results.  However, as angel mentioned, your sample SQL don't need the IsNull or coalesce the way its written.  The statement will return the same results with or without them.  

select * from company where comp_companyid = 2619 OR comp_companyid IS NULL  

This will give you both 2619 and NULL records.  Also, double check to make sure comp_companyid has been indexed.  That will improve performance too.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question