You are on page 1of 1

NVL always evaluates both arguments, while COALESCE stops evaluation whenever it

finds first non-NULL:


SELECT SUM(val)
FROM (
SELECT NVL(1, LENGTH(RAWTOHEX(SYS_GUID()))) AS val
FROM dual
CONNECT BY
level <= 10000
)
This runs for almost 0.5 seconds, since it generates SYS_GUID()'s, despite 1 bei
ng not a NULL.
SELECT SUM(val)
FROM (
SELECT COALESCE(1, LENGTH(RAWTOHEX(SYS_GUID()))) AS val
FROM dual
CONNECT BY
level <= 10000
)
This understands that 1 is not a NULL and does not evaluate the second argument.
SYS_GUID's are not generated and the query is instant.

You might also like