Skip to Main Content
  • Questions
  • Repetitive Function call in SQL - Performance Tunning

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Arul.

Asked: November 27, 2014 - 6:35 am UTC

Last updated: November 29, 2014 - 12:36 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi Tom,

Will calling a package function repetitively in SELECT clause impacts performance of a SQL?

Function returns DateTime from a table and in SELECT clause, Date (DD-MON-RRRR) is shown as separate column and the same function is called again to show the Time (HH24:MI:SS) as a separate column.
The function which relies on a table is a huge table and indexed properly.

I would like to know whether such repetitive call to the function in select clause will have performance impact.

As per my understanding, when a package function is called, function will be cached and repetitive call will use the code in cache.

In this case whether the result set evaluated during the first call will be reused for the subsequent calls or the result set is evaluated every time even though the WHERE
clause and parameters passed are same.

SQL 1 :
SELECT
TO_CHAR(fn_get_dt_tme(emp_id), 'DD-MON-RRRR') join_date, TO_CHAR(fn_get_dt_tme(emp_id), 'HH24:MI:SS') join_time
FROM emp;

I have rewritten the above query as below:

SQL 2:
SELECT TO_CHAR(join_dt, 'DD-MON-RRRR') join_date,
TO_CHAR(join_dt, 'HH24:MI:SS') join_time
FROM (SELECT fn_get_dt_tme(emp_id) join_dt FROM emp );

CREATE FUNCTION fn_get_dt_tme(p_emp_id NUMBER)
RETURN DATE AS
v_date DATE;
BEGIN
SELECT join_date INTO v_date FROM emp WHERE emp_id = p_emp_id;
RETURN v_date;
END;

Will there be performance improvement in the second query over the first query? kindly advise.

Thanks in advance.

Best Regards,
Arulkumar S

and Tom said...

... Will calling a package function repetitively in SELECT clause impacts performance of a SQL?
...

obviously? If you invoke something that takes a measurable amount of times, and you invoke it a lot, it will take a big amount of measurable time.


I do not approve of your approach here at all. Writing a plsql function that queries table T and is called by a SQL query that queries table T - that is "not smart".


Convince me this entire thing should not just be:


select to_char(join_dt,'dd-mon-yyyy'), to_char(join_dt, 'hh24:mi:ss' )
from emp;


why - WHY is there any plsql here at all. Especially plsql that would *query up the value of the current row from the sql statement*!!!!!!

You query emp.
You get the emp_id for row 1.
You pass that to a function.
and that function...... wait for it..... queries up row 1?????

Why (at the very least) would you not just pass the data from SQL down to plsql - why would you query it again??????


That is the performance killer here.


I hesitate to show you techniques for improving the performance of calling plsql from sql - because that approach is just wrong for you. You shouldn't be calling plsql *at all*, and you very surely, absolutely should not be calling plsql that queries up the row that the SQL statement just queried up!!!!

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library