Solved

Using COALESCE forces SQL to timeout

Posted on 2009-07-06
7
788 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
7 Comments
 
LVL 142

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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 142

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 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Date from a string 4 57
SQL help 5 50
SQl Agent job fails--SSIS package looses password 6 45
Getting max record but maybe not use Group BY 2 15
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
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.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now