Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

function based column gets called for unneeded rows (optimizer issue?)

rode_mbMar 18 2025

Hi,

we hit an issue and actually i'm not sure if it's a but or it's an expected behavior. you can test this example with the default EMP-Table…

For the test you will need a dummy function that makes a output every time it get's called:

CREATE OR REPLACE FUNCTION put_line_error(empno IN NUMBER) RETURN NUMBER IS
BEGIN
  dbms_output.put_line('Test: ' || empno);
  RETURN 1;
END;
/

That's the test:

SQL> set serveroutput on;
SQL> SELECT empno -- result 4 rows
2 FROM (SELECT emp_filtered.empno, -- result 4 rows
3 put_line_error(empno => emp_filtered.empno) AS function_value -- called 4 times
4 FROM (SELECT empno -- result 4 rows
5 FROM emp -- 14 rows
6 WHERE emp.job = 'CLERK') emp_filtered)
7* WHERE function_value = 1;

EMPNO 
________ 
7369 
7876 
7900 
7934 

Test: 7369
Test: 7876
Test: 7900
Test: 7934

SQL> SELECT empno -- result 4 rows
2 FROM (SELECT emp_filtered.empno, -- result 4 rows
3 put_line_error(empno => emp_filtered.empno) AS function_value -- called 14 times
4 FROM (SELECT empno -- result 4 rows
5 FROM emp -- 14 rows
6 WHERE emp.job = (SELECT 'CLERK'
7 FROM dual)) emp_filtered)
8* WHERE function_value = 1;

EMPNO 
________ 
7369 
7876 
7900 
7934 

Test: 7369
Test: 7499
Test: 7521
Test: 7566
Test: 7654
Test: 7698
Test: 7782
Test: 7788
Test: 7839
Test: 7844
Test: 7876
Test: 7900
Test: 7902
Test: 7934

SQL> 

As you can see in the second example the function gets called more often in the second example.

Any ideas or suggestions if this is expceted or a bug? Actually I'm not happy with the idea of expected behavior.

There is a workaround at least for this example if you create an index on EMP(JOB). But I'm not sure if this is the all-time solution for this problem.

Thanks in advance!

Regards,

Roland

Comments

Post Details

Added on Mar 18 2025
3 comments
92 views