28 Nov 2012

Oracle Pipelined Table functions

Oracle Pipelined Table functions
Do you want to know/learn?
ü What are Oracle Table functions?
ü How could these tables be pipelined, parallelized and streamed?
ü What are the practical benefits of using these functions?
This article intends to provide an overview of the Pipelined Table functions introduced in Oracle 9i.
Table functions in Oracle:
ü Table functions are functions that take a collection of rows as input and produce a collection of rows (either a nested table or a varray) as the output.
ü An input collection parameter can be either a collection type (such as a VARRAY or a PL/SQL table) or a REF CURSOR.
ü They can be queried like a table by calling the function in the FROM clause of a query.
ü They can be assigned to a PL/SQL collection variable by calling the function in the SELECT list of a query.
Pipelined Table functions in Oracle:
ü A pipelined table function can return the table function's result collection in subsets. The returned collection behaves like a stream that can be fetched on demand.
This makes it possible to use a table function like a virtual table.
ü ‘PIPELINED’ keyword indicates that the function will return rows iteratively.
ü PIPE ROW’ statement causes a table function to pipe a row and continue processing.
ü The statement enables a PL/SQL table function to return rows as soon as they are produced.
ü A pipelined table function must have a blank RETURN statement that does not return a value. The RETURN statement transfers the control back to the consumer and ensures that the next fetch gets a NO_DATA_FOUND exception.
Let us try to understand the above keywords and the pipelined function better with the help of a simple illustration.
Illustration:
Requirement:
Write a function to convert the input of the following type (Ticker, Open Price, Close Price) to a two-step output of the following types(Ticker, PriceType, Price)
i.e, Conversion must be from (“ORCL”, 41, 42) to (“ORCL”, “O”, 41) and (“ORCL”, “C”,42)
Please refer to the attached ‘Pipelined_Functions.sql’, for understanding the table structure, input and output types we will be using in the following example.
Solution through Pipelined functions:
CREATE FUNCTION StockPivot(p refcur_pkg.refcur_t) RETURN TickerTypeSet
PIPELINED IS -- indicates that the function will return rows iteratively
out_rec TickerType := TickerType(NULL,NULL,NULL);
in_rec p%ROWTYPE;
BEGIN
LOOP
FETCH p INTO in_rec;
EXIT WHEN p%NOTFOUND;
-- first row
out_rec.ticker := in_rec.Ticker;
out_rec.PriceType := 'O';
out_rec.price := in_rec.OpenPrice;
PIPE ROW(out_rec); -- pipes the first row of the two-step output
-- second row
out_rec.PriceType := 'C';
out_rec.Price := in_rec.ClosePrice; -- pipes the second row of the two-step output
PIPE ROW(out_rec);
END LOOP;
CLOSE p;
RETURN; -- Blank return statement to indicate the transfer of control back to the caller
END;
How does this Pipelining differ from our conventional approach:
Consider the table to have 2 rows, so we are now expecting 4 rows from this function. In both the methods, let us compare the time when these rows are returned. Source.
Conventional Approach
Pipelining Approach
Time the function was called: 07:02:25.000
Time the function was called: 07:02:35.359
Row Number
Time Returned
Comments
1
07:02:30.062
First row takes 5 sec
2
07:02:30.062
3
07:02:30.062
4
07:02:30.062
Row Number
Time Returned
Comments
1
07:02:36.390
First row takes 1 sec
2
07:02:37.390
3
07:02:38.390
4
07:02:39.390
Advantages & Other features:
ü Improved query response time: Normal functions wait for the entire result set to be constructed and then return to the server, the entire result at once. Whereas, here the rows are returned as and when processed, thereby improving the query response time.
ü Saves memory: Consider for Eg., we have one lakh rows as input to an non-pipelined function, we need to store the two lakh output rows in our PGA memory before it is returned as a single result set. Here, since we are iteratively sending the output rows in subsets, we save a huge amount of memory.
ü Eliminates intermediate staging: Pipelined table functions takes bulk data set as input and makes all the necessary transformation within the function and has the ability to stream the output result set to the next process without intermediate staging. This process is referred to as Streaming. More on streaming.
ü Improved performance by parallelization: pipelined functions can be parallelized by Oracle which means that PL/SQL can be executed by multiple slaves in parallel, thereby reducing the processing time drastically. More on parallelization.
As a gist, Oracle Table functions can be considered to bring in significant performance improvements by using the concepts of Pipelining, Parallelization and Streaming!

6 Nov 2012

What is the difference between MTL_SYSTEM_ITEMS,MTL_SYSTEM_ITEMS_B tables?



What is the difference between MTL_SYSTEM_ITEMS,MTL_SYSTEM_ITEMS_B tables?
mtl_system_items is a synonym that points to mtl_system_items_b

mtl_system_items used to be the actual items table in older versions.
But in recent version the mtl_system_items_b is the items table. If any old code was using mtl_system_items, the synonym helps it compile and thus make this change backward compatible.

Oracle follows the following convention to handle multi-language.
The b stands for base table. And then there is a table called tl (e.g. mtlsystem_items_tl). In the _tl table, for each base record, you can find multiple records - one for each language that you use.

MTL_SYSTEMS_B is the base table and MTL_SYSTEM_ITEMS is a synonym for MTL_SYSTEM_ITEMS_B.Because of this synonym, old code written that used mtl_system_items still compiles without any problem. That is the sole purpose of the synonym.

What is
MTL_SYSTEM_ITEMS_B table?
MTL_SYSTEM_ITEMS_B is the definition table for items. This table holds the definitions for inventory items, engineering items, and purchasing items. You can specify item-related information in fields such as: Bill of Material, Costing, Purchasing, Receiving, Inventory, Physical attributes, General Planning, MPS/MRP Planning, Lead times, Work in Process, Order Management, and Invoicing.

You can set up the item with multiple segments, since it is implemented as a flexfield. Use the standard 'System Items' flexfield that is shipped with the product to configure your item flexfield.
The primary key for an item is the INVENTORY_ITEM_ID and ORGANIZATION_ID. Therefore, the same item can be defined in more than one organization.

Each item is initially defined in an item master organization. The user then assigns the item to other organizations that need to recognize this item. A row is inserted for each new organization the item is assigned to. Many columns such as MTL_TRANSACTIONS_ENABLED_FLAG and BOM_ENABLED_FLAG correspond to item attributes defined in the MTL_ITEM_ATTRIBUTES table.
Two unit of measure columns are stored in MTL_SYSTEM_ITEMS_B table. PRIMARY_UOM_CODE is the 3-character unit that is used throughout Oracle Manufacturing. PRIMARY_UNIT_OF_MEASURE is the 25-character Unit of Measure that is used throughout Oracle Purchasing. Unlike the PRIMARY_UOM_CODE, the Unit of Measure is language-dependent attribute, however, PRIMARY_UNIT_OF_MEASURE column stores value in the installation base language only.