[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1562
  • Last Modified:

A question related to NVL

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

Amit
0
Sys_Prog
Asked:
Sys_Prog
1 Solution
 
riazpkCommented:
Quite Intresting question...


To my knowledge:

the NVL function will be performed for each and every row.
0
 
schwertnerCommented:
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, '' ) ;
.....
begin
......
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.

0
 
grim_toasterCommented:
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...
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
grim_toasterCommented:
As mentioned in previous post, this may help you (or be completely useless) :)

http://www.experts-exchange.com/Databases/Oracle/Q_20790890.html
0
 
Sys_ProgAuthor Commented:
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

Amit
0
 
vanmeerendonkCommented:
Just another testcase to get you thinking:
Table Test with 1 column with numbers with 4 rows
10
10
10
5
Function testrow:
create or replace function testrow (p_in number) return number
is
begin
  dbms_application_info.set_client_info(nvl(userenv('CLIENT_INFO'),0)+1);
  return p_in;
end;

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

Then:
3 rows returned and client_info-column in V$session says 4
So:
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

0
 
seazodiacCommented:
Sys_Prog:

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.
0
 
Sys_ProgAuthor Commented:
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

Amit
0
 
Sys_ProgAuthor Commented:
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]

Anyways,

Thanks again

Amit

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now