What is the fastest in an ORACLE database ?
Call a function inside a select statement to retrive a single value for each row
SELECT field1, field2, F_GET_LIBELLE_STATUT( field2 ) FROM table1 WHERE ...
with the simple function :
create or replace
FUNCTION "F_GET_LIBELLE_STATUT" (v_statut NUMBER) RETURN VARCHAR2 IS
tmpVar VARCHAR2(40);
BEGIN
select libelle into tmpVar from t_statut_ope where id_statut = v_statut;
RETURN tmpVar;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN '';
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END f_get_libelle_statut;
Or doing a join in the select statement ?
Select a.field1, a.field2, b.libelle
FROM table1 a LEFT JOIN t_statut_ope b ON b.id_statut = a.field2
WHERE ...
And is it the same answer if I call a lot of functions (ten or more) in the select and in the clause condition ?
Thanks for your answer.
Anything which can be done in SQL should be done in SQL (*).
A join will always be faster than calling that function because set-based operations are always faster than row by agonizing row. Then you have the overhead of moving between SQL and PL/SQL. Plus the overhead of handling those NO_DATA_FOUND exceptions, which aren't truly exceptions because gaps are expected and tolerated. Finally, the optimizer will pick a better plan for the pure SQL option because you have given it all the information it needs.
(*) I answered this question way back when and I answered it focused on the example given, and I stand by that answer. If we can get the data we need using SQL against tables (via joins, views, inline views, subqueries) then we should do that. But I want to drill down into the underlying question: why does Oracle support the use of functions in queries? Because sometimes we need to do things we can't do in SQL.
Here are use cases for calling functions that execute SQL (I am thinking primarily of functions invoked through table()
calls ) instead of using a join (or other SQL constructs such as inline view, WITH clause) :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With