SQL COALESCE() Very Cool, But Slower Than ISNULL()

Posted August 4, 2006 at 5:25 PM by Ben Nadel

Tags: SQL

As I just blogged, COALESCE() was just pointed out to me. Its a cool function, but I was curious to see how it compared in speed to ISNULL(). It seems that COALESCE() is about 2 to 3 times slower on enormous queries:

  • <!--- Test the COALESCE() SQL method. --->
  • <cftimer label="COALESCE" type="outline">
  •  
  • <cfquery name="qCoalesce" datasource="...">
  • SELECT
  • id,
  • COALESCE( date_created, getDate() ) AS date_created
  • FROM
  • web_stats_hit
  • WHERE
  • COALESCE( date_created, getDate() ) IS NOT NULL
  • AND
  • COALESCE( id, 0 ) IS NOT NULL
  • </cfquery>
  •  
  • </cftimer>
  •  
  • <!--- Test the ISNULL() SQL method. --->
  • <cftimer label="ISNULL" type="outline">
  •  
  • <cfquery name="qIsNull" datasource="...">
  • SELECT
  • id,
  • ISNULL( date_created, getDate() ) AS date_created
  • FROM
  • web_stats_hit
  • WHERE
  • ISNULL( date_created, getDate() ) IS NOT NULL
  • AND
  • ISNULL( id, 0 ) IS NOT NULL
  • </cfquery>
  •  
  • </cftimer>

The ISNULL() method performed on average at about 550 ms. The COALESCE() method performed on average at about 1500 ms. The other interesting thing was that ISNULL() was very very consistent. COALESCE() on the other hand, would range from 1000 ms to 2000 ms from test to test.

I am not doing this to point out that one is better than the other; they do different things. I am just saying, consider one or the other based on the situation.



Reader Comments

Aug 7, 2006 at 8:10 AM // reply »
3 Comments

So, what do you recommend? I am use isNull() a lot, especially when working with date. But not on enourmous queries. Do you think I should switch to coalesce?


Sep 29, 2006 at 2:37 PM // reply »
1 Comments

COALESCE is actually very useful in certain situations. For example, if you had a table with two quantity columns and you wanted to sum the difference between them, you would do,

select sum(coalesce(quanitty1, 0) - coalesce(quantity2, 0)) as difference

If you didn't use coalesce, then the sum will result in a NULL if any quantity1 or quantity2 field is NULL.

I know this comment is a little late, but I found this blog doing a google search and thought I might add to the discussion. :-)


Sep 29, 2006 at 4:08 PM // reply »
11,314 Comments

Leon,

I appreciate what you are saying, however I think the same thing could be done with ISNULL as well:

select sum(ISNULL(quanitty1, 0) - ISNULL(quantity2, 0)) as difference

I am not argueing one way or the other. From what people have said, COALESCE is more of a standard across database management systems.


Dec 19, 2006 at 5:10 PM // reply »
1 Comments

Coalesce is much more useful for something like this

DECLARE @CodeList varchar(1000)

SELECT @CodeList = COALESCE(@CodeList +', ', '') +
CAST(EmpCode AS varchar(20))
FROM Employees

SELECT @CodeList

Where EmpCode is a column in the Employees table, this will return a comma-delimited list of the employees' codes


Dec 20, 2006 at 7:30 AM // reply »
11,314 Comments

Dave,

Nice tip. I have never really worked on building a string using a SELECT statement, but I have seen it done and it's probably something I should try.


Oct 15, 2007 at 10:06 AM // reply »
1 Comments

I would say isnull() is 'as' standard as coalesce(). Oracle and Sybase use it just like MS..

They are the same 'unless' you're testing multiple values. That's the ONLY reason you should use coalesce() ...unless of course its for code consistency.


Nov 23, 2009 at 4:01 AM // reply »
1 Comments

Actually, it can be done by ISNULL as well.

DECLARE @CodeList varchar(1000)

SELECT @CodeList = ISNULL(@CodeList +', ', '') +
CAST(EmpCode AS varchar(20))
FROM Employees

SELECT @CodeList


Oct 8, 2010 at 4:35 PM // reply »
1 Comments

COALESCE has 2 major advantages:
1) it is part of the ANSI 2000 standard which can
make your sql more portable. This, however, is pointless to most people, as they usually work in exactly one environment.
2) you are not limited to two values.

ISNULL, logically speaking, should perform better in most cases, although there are some occasions where that does not seem to be the case.


Oct 10, 2010 at 3:38 PM // reply »
11,314 Comments

@Jeremy,

I switched to MySQL a few years ago and have since starting using COALESCE() with success. MySQL does have IsNull() functions, but they don't act the same way - they are simply null-value-checkers.


Apr 14, 2011 at 8:09 AM // reply »
1 Comments

@Ben: Right, MySQL's ISNULL() behaves diffently to MSSQL. But shouldn't ISNULL() return whether a value is NULL or not?
For the desired result MySQL provides IFNULL(), which behaves like ISNULL() in MSSQL and has the 'correct' function naming.


Apr 19, 2011 at 4:23 PM // reply »
1 Comments

Hi,

None of code of David or Wayos S. working in Sybase ASE. It always return last value from the list. Any ideas?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 20, 2013 at 6:56 AM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
" Meaning, if I PUT an object into S3, can I (as the PUT executer) read that object from S3 immediately? " "It depends" : http://aws.amazon.com/s3/faqs/#What_data_consistency_mo ... read »
Jun 20, 2013 at 3:15 AM
A Billion Wicked Thoughts By Ogi Ogas And Sai Gaddam
nice post i love it thanks 4 u :) ... read »
seb
Jun 20, 2013 at 2:32 AM
Working With Inherited Collections In AngularJS
@mike, @ben, The best article about scope and prototypal prototypical inheritance in angularjs is http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical- ... read »
Jun 20, 2013 at 2:17 AM
ColdFusion NumberFormat() Exploration
Nice read thanks Ben, Is there a way to mask a negative number? Long story short in the finance sector when you go 'short' on a stock you want the price to fall this is a good thing because you are ... read »
Jun 20, 2013 at 1:09 AM
The Beauty Of The jQuery Each() Method
my html code : <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="nss.js"> ... read »
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools