Tuesday, 10 December 2013

Pipelined Table Functions

Pipelined Table Functions for Multiple Transformations

Chaining pipelined table functions is an efficient way to perform multiple transformations on data.
             You cannot run a pipelined table function over a database link. The reason is that the return type of a pipelined table function is a SQL user-defined type, which can be used only in a single database Although the return type of a pipelined table function might appear to be a PL/SQL type, the database actually converts that PL/SQL type to a corresponding SQL user-defined type.

Overview of Table Functions

table function is a user-defined PL/SQL function that returns a collection of rows (a nested table or varray). You can select from this collection as if it were a database table by invoking the table function inside the TABLE clause in a SELECT statement. For example:
SELECT * FROM TABLE(table_function_name(parameter_list))
To improve the performance of a table function, you can:
  • Enable the function for parallel execution, with the PARALLEL_ENABLE option.
    Functions enabled for parallel execution can run concurrently.
  • Stream the function results directly to the next process, with Oracle .
    pipelined table function returns a row to its invoker immediately after processing that row and continues to process rows. Response time improves because the entire collection need not be constructed and returned to the server before the query can return a single result row. (Also, the function needs less memory, because the object cache need not materialize the entire collection.)
    Creating and Invoking Pipelined Table Function
    CREATE OR REPLACE PACKAGE pkg1 AS
      TYPE numset_t IS TABLE OF NUMBER;
      FUNCTION f1(x NUMBER) RETURN numset_t PIPELINED;
    END pkg1;
    /
    
    CREATE PACKAGE BODY pkg1 AS
      -- FUNCTION f1 returns a collection of elements (1,2,3,... x)
      FUNCTION f1(x NUMBER) RETURN numset_t PIPELINED IS
      BEGIN
        FOR i IN 1..x LOOP
          PIPE ROW(i);
        END LOOP;
        RETURN;
      END f1;
    END pkg1;
    /
    
    SELECT * FROM TABLE(pkg1.f1(5));
    Result:-
    COLUMN_VALUE
    ------------
               1
               2
               3
               4
               5
    
    
    For Details Reference

No comments:

Post a Comment