I have been wanting to know for some time, how the internal working is done for DECODE and NVL function.
Does the DECODE function have more overhead in case the number of arguments in it are more?
Also, does the NVL function evaluate the value given instead of NULL, even if the original value is not NULL
e.g. for NVL(COL1, func_call())
would the func_call() be calculated even if COL1 is not NULL. In that case, it might create performance issue if the COL1 is rarely null and func_call() is time consuming.
Thanks in advance
NVL always evaluates both arguments, so even if COL1 is not null it will evaluate func_call()
Decode does short circuit, so the first valid condition stops execution.
With similar arguments performance is pretty similar between the two. I usually just choose based on simplicity – NVL unless I need to have a if-then-else type construct.
—-EDIT—-
In situations where you do use NVL if you find yourself doing nested NVL like nvl(col1, nvl(col2, ‘0’)) take a look at the coalesce function where this would be expressed as coalesce(col1, col2, ‘0’) – returns the first non-null argument