Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions vs procedures in Oracle

can anybody explain what is the main difference between functions and procedures in Oracle? Why must I use procedures if I can do everything with functions?

  1. If I cannot call procedure in sql statement, ok, I'll write a function to do the same work.
  2. Procedures don't return values, ok, I'll return only sql%rowcount or 1(success), 0(exception) after any dml operation
  3. Both procedures and functions can pass variables to calling environment via OUT/IN OUT parameters

I heard that the main difference is in performance, 'procedures are faster than functions'. But without any detail.

Thanks in advance.

like image 265
0bj3ct Avatar asked Aug 21 '14 06:08

0bj3ct


People also ask

What is difference between function and procedure in Oracle?

A function returns a value and control to calling function or code. A procedure returns the control but not any value to calling function or code. A procedure has support for try-catch blocks. A select statement can have a function call.

What are functions and procedures in Oracle?

A procedure is a subprogram that performs a specific action. You specify the name of the procedure, its parameters, its local variables, and the BEGIN-END block that contains its code and handles any exceptions. A function is a subprogram that computes and returns a value.

What is the difference between procedure vs functions?

A function would return the returning value/control to the code or calling function. The procedures perform certain tasks in a particular order on the basis of the given inputs. A procedure, on the other hand, would return the control, but would not return any value to the calling function or the code.

Which is better function or procedure?

Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it. Procedures can not be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.


2 Answers

The difference is- A function must return a value (of any type) by default definition of it, whereas in case of a procedure you need to use parameters like OUT or IN OUT parameters to get the results. You can use a function in a normal SQL where as you cannot use a procedure in SQL statements.

Some Differences between Functions and Procedures

  1. A function always returns a value using the return statement while a procedure may return one or more values through parameters or may not return at all.Although, OUT parameters can still be used in functions, they are not advisable neither are there cases where one might find a need to do so. Using OUT parameter restricts a function from being used in a SQL Statement.

  2. Functions can be used in typical SQL statements like SELECT, INSERT, UPDATE, DELETE, MERGE, while procedures can't.

  3. Functions are normally used for computations where as procedures are normally used for executing business logic.

  4. Oracle provides the provision of creating "Function Based Indexes" to improve the performance of the subsequent SQL statement. This applies when performing the function on an indexed column in where clause of a query.

More Information on Functions Vs. Procedures here and here.

like image 165
Romo Daneghyan Avatar answered Sep 28 '22 22:09

Romo Daneghyan


There is almost never a performance difference between procedures and functions.

In a few extremely rare cases:

  • A procedure IN OUT argument is faster than a function return, when inlining is enabled.
  • A procedure IN OUT argument is slower than a function return, when inlining is disabled.

Test code

--Run one of these to set optimization level: --alter session set plsql_optimize_level=0; --alter session set plsql_optimize_level=1; --alter session set plsql_optimize_level=2; --alter session set plsql_optimize_level=3;  --Run this to compare times.  Move the comment to enable the procedure or the function. declare     v_result varchar2(4000);      procedure test_procedure(p_result in out varchar2) is     begin         p_result := '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';     end;      function test_function return varchar2 is     begin         return '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';     end; begin     for i in 1 .. 10000000 loop         --Comment out one of these lines to change the test.         --test_procedure(v_result);         v_result := test_function;     end loop; end; / 

Results

Inlining enabled:  PLSQL_OPTIMIZE_LEVEL = 2 (default) or 3 Function  run time in seconds: 2.839, 2.933, 2.979 Procedure run time in seconds: 1.685, 1.700, 1.762  Inlining disabled: PLSQL_OPTIMIZE_LEVEL = 0 or 1 Function  run time in seconds:  5.164, 4.967, 5.632 Procedure run time in seconds: 6.1, 6.006, 6.037 

The above code is trivial and perhaps subject to other optimizations. But I have seen similar results with production code.

Why the difference doesn't matter

Don't look at the above test and think "a procedure runs twice as fast as a function!". Yes, the overhead of a function is almost twice as much as the overhead of a procedure. But either way, the overhead is irrelevantly small.

The key to database performance is to do as much work as possible in SQL statements, in batches. If a program calls a function or procedure ten million times per second then that program has serious design problems.

like image 26
Jon Heller Avatar answered Sep 28 '22 23:09

Jon Heller