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

user2259597 picture user2259597 · Apr 9, 2013 · Viewed 17.7k times · Source

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?

Answer

Bohemian picture Bohemian · Apr 9, 2013

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.