Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MySQL, which is more efficient: IFNULL or NULLIF?

Tags:

These two MySQL functions do the same thing:

IFNULL(column_name, 'test') = 'test' 

or

NULLIF(column_name, 'test') IS NULL 

Which one is more efficient?

like image 408
user2259597 Avatar asked Apr 08 '13 23:04

user2259597


People also ask

What is difference between Isnull and Ifnull in mysql?

IFNULL is equivalent to ISNULL. IFNULL is equivalent to COALESCE except that IFNULL is called with only two arguments. ISNULL(a,b) is different from x IS NULL . The arguments can have any data type supported by Vertica.

What is the use of Ifnull () operator in mysql?

The IFNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.

Why is Nullif used?

We often use the NULLIF function when the database contains “special” values such as zero or empty string that we want to handle them as NULL values. This is very useful when we use the aggregate functions such as AVG, MAX, MIN, SUM, and COUNT.

How does Nullif work in SQL?

The NULLIF() function returns NULL if two expressions are equal, otherwise it returns the first expression.


1 Answers

They are both as efficient as each other - the functions have about the same overhead as each other.

But this is more efficient than either:

(column_name is null   or column_name = 'test') 

Because the function doesn't need to be invoked.

You may find putting the more commonly encountered test first improves performance.


With questions like this, the simplest and more reliable way to discover relative performance is to just try them and compare query timings. Make sure you have production-sized and realistic-valued datasets so the test is fair.

like image 96
Bohemian Avatar answered Sep 21 '22 01:09

Bohemian