For fun I been playing with the built-in Optimizer for Oracle in Toad today. One of the optimizations it suggests is the following
AND emp.pay_type = NVL('FT', UID)
Instead of
AND emp.pay_type = 'FT'
I’m confused on what is happening here and because of it, confused on also on why this would improve performance. Since FT is a string literal within the SQL query and therefore never NULL, why would this make any difference? I’m guessing it has something to do with the existing Index on the field, but am unable to find anything in the Oracle docs.
That is weird advice. The NVL function works like this:
If ‘exp1’ is not null, it is returned; otherwise ‘val1’ is returned.
Since ‘FT’ in the example cannot be NULL, there is no benefit to using the NVL function, and a small performance penalty (at least for the optimizer to work out that NVL is redundant; maybe an execution penalty if the optimizer does not work out that NVL is redundant).
If the condition read:
AND emp.pay_type = NVL(“FT”, UID)
then there might be a benefit; here we have a delimited identifier (column name enclosed in double quotes) and the column value could perhaps be NULL; the NVL call ensures that NULL is returned only if “FT” is NULL and UID is NULL. UID is, of course, a regular identifier.
It might make sense if the condition read:
Now if the UID value is NULL, then a default value ‘FT’ is used as the corresponding pay_type.