Solved

Using COALESCE forces SQL to timeout

Posted on 2009-07-06
7
813 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 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
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 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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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