Skip to Main Content
  • Questions
  • avoiding calling functions for every row in select

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Khalid.

Asked: March 13, 2016 - 9:21 pm UTC

Last updated: March 14, 2016 - 2:24 am UTC

Version: 11.2

Viewed 1000+ times

You Asked

Hi Tom,

I need ideas to tune codes.

How to avoid calling pl/sql function for each row in select / update statement for example:-

select empno, get_name(empno) from employees;

and get_name is a function that will retrieve the names of the employee "just an example forget about normalization"

or update employees set name = get_name(empno) where condition;

This is an example but in my company all the developers is using same technique and complaining about slowness.

I was thinking about bulk process but I will end up with select statements for each row in the collection and same problem happen again.

in oracle documentation it tell me to change the SQL statement it self "join the base table with the lookup tables" to avoid the select inside the loop but there is a lot of lookup tables

for example select empno, get_name(empno), get_address(address_id), get_email(email_id) from employees;

so for every column they have to bring the data by calling a procedure

we are dealing with millions of rows in the select or update statements.

Regards,
Khalid.

and Connor said...

select empno, get_name(empno) from employees;

"This is an example but in my company all the developers is using same technique and complaining about slowness."

Yup...common problem. People write bad slow code...and then complain that things are bad and slow. Funny that how happens :-)

What your developers are doing is effectively re-inventing the JOIN feature in SQL with their own code. So priority #1 is to either

- educate them better, or
- get better educated developers

Having said that, some things you can look at doing as to ease the pain

1) subquery cache,

change

select empno, get_name(empno) from employees;

to

select empno, ( select get_name(empno) from dual ) from employees;

2) result cache

See http://www.oracle-developer.net/display.php?id=504

But ultimately, go back and look at priority #1 :-)


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