A question related to NVL

Posted on 2003-11-18
Last Modified: 2012-05-05
Hi Experts,

I have a question regarding Using functions in where clasue, in fact it is a question for where clause

Suppos, I have a query like

SELECT    count ( ROWID )
   FROM    table1
  WHERE   Upper ( Trim ( col1 ) ) = NVL ( :as_para, '' )

Now is the NVL operation performed for every row in table1 while comparing OR it is performed only once and then used for all the rows later

Thanks in advance

Question by:Sys_Prog
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 13

Expert Comment

ID: 9776590
Quite Intresting question...

To my knowledge:

the NVL function will be performed for each and every row.
LVL 48

Expert Comment

ID: 9776928
I am sure that Oracle will not calculate NVL function in every row selection if the argument is a bind variable (parameter).
But if you hesitate you can ensure that it is so. I base my consideration on the fact you use bind variable.
It seems to be parameter in a procedure or item in Forms, reports

Do this

v_param     Varchar2(90) :=  NVL ( :as_para, '' ) ;
SELECT    count ( ROWID )
  FROM    table1
 WHERE   Upper ( Trim ( col1 ) ) = v_param    

You have not to bother about the function NVL. It is very simple gunction.
In contrast to SIN(X) = x/1! - x**3/3! +x**5/5! + ..........
and other "monster" functions.


Expert Comment

ID: 9776952
It will only be computed once.  The thing you will have to worry about with this one is what you are doing to the col1 column, Upper ( Trim ( col1 ) ) will be done for every row.  

A better performing option (sometimes) would be to do:

SELECT    count (1)
   FROM    table1
  WHERE   :as_para IS NULL
        OR   Upper ( Trim ( col1 ) ) = :as_para

So at least you wouldn't be doing your upper(trim()) if the passed in value is null - but it would depend on how often it is run with null, or when not null.  Ah, but in fact you're using bind variables, so, that's sometimes a bit of a grey area (depending on which version of Oracle you are using).  If it is vital that the text be in the Upper'ed format and trimmed, it may be worth putting a function based index on the table, or the possibility of converting the value to always be uppercased, and trimmed when inserted into the database (possibly by a trigger).

There were a couple of posts on here recently for the use of OR and the bind variables stuff, I'll try to find them for you...
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 9776961
As mentioned in previous post, this may help you (or be completely useless) :)
LVL 10

Author Comment

ID: 9776994
Hi Guys,

Thanks for your quick response

I am using Oracle 9i and yes, it is a local variable. Also, I am trigerring this Select statement as an embedded SQL from PB.

In this case, will the NVL be executed on every row OR only once. In either case, if someone could provide me with some link, it would be of great help

Thanks in adv


Accepted Solution

vanmeerendonk earned 125 total points
ID: 9778095
Just another testcase to get you thinking:
Table Test with 1 column with numbers with 4 rows
Function testrow:
create or replace function testrow (p_in number) return number
  return p_in;

select * from table where p_in = testrow(:b);   --> :b_in is 10

3 rows returned and client_info-column in V$session says 4
the function is executed for every row.

When you index the column, then you see that the function is not executed for every row.
NVL is just another (in this case kernel-)function.
In your case I would say the NVL goes for every row

LVL 23

Expert Comment

ID: 9779277

The NVL function will, without any doubts, perform for each and every row. You using bind variable has nothing to do with the number of times it executes, it only affect the number of the parse times.
LVL 10

Author Comment

ID: 9784841
Hi all,

logically speaking, oracle should execute only once because it's a variable and not a column.

vanmeerendonk's  answer was really really good, just have one doubt after that, are user-defined functions treated the same way as the built-in functions.

Thanks in adv

LVL 10

Author Comment

ID: 9919924
Hi Guys,

Not yet confirmed of the answer but really liked  vanmeerendonk's approach,

Therefore accepted his answer [It's for his approach and not the answer because I am yet to confirm that]


Thanks again



Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This post first appeared at Oracleinaction  ( Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question