Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pl/sql function called how many times?

Assume you have the following update:

Update table set col1 = func(col2)
where col1<>func(col2)

The func function is evaluated two times for every row, or once for every row?

Thanks,

like image 765
Florin stands with Ukraine Avatar asked Jul 21 '11 12:07

Florin stands with Ukraine


People also ask

Can PL SQL function return more than one value?

A pl/sql function can return multiple values by using the pipe row or we call it as PIPELINED FUNCTIONS.

Can a function return 2 values Oracle?

There is no way you can return 2 variable. It has to be one. You can use a custom rec type or array which you can return from the function. Save this answer.

Will function return more than one value in Oracle?

2) Yes. In functions are allowed using IN and OUT parameters.

How many types of functions are there in PL SQL?

The list of Oracle/PLSQL functions is sorted into the type of function based on categories such as string/character, conversion, advanced, numeric/mathematical, and date/time. These functions can be used in SQL statements or queries in Oracle.


3 Answers

This is the kind of situation where some experimentation is useful (this was conducted on 10g). Using the following query, we can tell that normal functions, using the same parameters (in this case, none) will be executed each time they are called:

select dbms_random.value() from all_tables

This is because Oracle assumes that a function will not return the same value consistently unless you tell it otherwise. We can do that by creating a function using the deterministic keyword:

CREATE FUNCTION rand_det
   RETURN NUMBER
   DETERMINISTIC AS
BEGIN
   RETURN DBMS_RANDOM.VALUE ();
END;

Using this function instead of dbms_random in the first query tells us that the query is being executed only once, despite the many calls. But this only clarifies the select section. What if we use the same deterministic function in both a select and a where clause. We can test that using the following query:

SELECT rand_det
FROM   all_tables
WHERE  rand_det > .5;

You may have to run this several times to see our proof, but, eventually, you'll see a list of values less than 0.5. This provides us with evidence that even the deterministic function is being executed twice: once for each section it appears in. As an alternative, you can modify our deterministic function as follows, then run the subsequent query, which will reveal 2 lines written to DBMS_OUTPUT.

CREATE OR REPLACE FUNCTION rand_det
   RETURN NUMBER
   DETERMINISTIC AS
BEGIN
   DBMS_OUTPUT.put_line ('Called!');
   RETURN DBMS_RANDOM.VALUE ();
END;

SELECT rand_det
FROM   all_tables;
like image 110
Allan Avatar answered Oct 07 '22 07:10

Allan


While I like Allan's answer for showing how to investigate this, I think the real lesson to take away is that you shouldn't rely on the answer to this question if you can avoid it.

Here's a useful post on the topic from Tom Kyte ("I've written thousands of times that you cannot rely on how many times or when or if SQL will call your function.") Even prior to 11g introducing the result cache, there are no guarantees about how many times a function will be called in processing a SQL statement. It can depend on the execution plan, which can change over time.

If your concern is performance and your function is deterministic, the 11g result cache is probably sufficient -- it won't guarantee a specific limit on the number of calls to the function, but should reduce the number of redundant calls significantly. (See link provided in @cularis answer.)

If for some reason you actually want to ensure that the two calls to the functions are distinct, I think the only way you could force that would be to add a second parameter to the function that is actually ignored but serves to prevent the result cache or optimizer from seeing the calls as identical.

like image 35
Dave Costa Avatar answered Oct 07 '22 08:10

Dave Costa


For 11g, it will be called once for every new occurence of col2. For the next calls, a cached result is used if caching is enabled.

like image 2
Jacob Avatar answered Oct 07 '22 07:10

Jacob