CLOB Performance Issue in PLSQL - oracle

I've created the below function . When we write SQL queries using this function, the query returning 10K records is running for a very long time 2 hours. I figured out the performance Issue is due to CLOB usage in the function. When I change the CLOB reference in the below type and function to Varchar2 the query runs in a sec.
But, I don't want to change the data type to Varchar2. I wanted to have it as CLOB. But how can I improve the performance?
I do understand that I can go with LISTAGG or other oracle provided string functions. But I wanted the data type as CLOB and wanted to know, why there is such a huge difference in execution.
Below is the code of the User Defined Function. The query sample is given below.
select my_concat(col1),my_concat(col2),my_concat(col3),my_concat(col4) from table group by col1,col2,col3,col4;
CREATE OR REPLACE TYPE "my_concat"
AUTHID CURRENT_USER
AS OBJECT (
curr_str clob,
STATIC FUNCTION odciaggregateinitialize (sctx IN OUT my_concat)
RETURN NUMBER,
MEMBER FUNCTION odciaggregateiterate (
SELF IN OUT my_concat,
p1 IN varchar2
)
RETURN NUMBER,
MEMBER FUNCTION odciaggregateterminate (
SELF IN my_concat,
returnvalue OUT clob,
flags IN NUMBER
)
RETURN NUMBER,
MEMBER FUNCTION odciaggregatemerge (
SELF IN OUT my_concat,
sctx2 IN my_concat
)
RETURN NUMBER
);
/
CREATE OR REPLACE TYPE BODY "my_concat"
IS
STATIC FUNCTION odciaggregateinitialize (sctx IN OUT my_concat)
RETURN NUMBER
IS
BEGIN
sctx := my_concat (NULL);
RETURN odciconst.success;
END;
MEMBER FUNCTION odciaggregateiterate (
SELF IN OUT my_concat,
p1 IN varchar2
)
RETURN NUMBER
IS
BEGIN
IF (curr_str IS NOT NULL)
THEN
curr_str := curr_str || ',' || p1;
ELSE
curr_str := p1;
END IF;
RETURN odciconst.success;
END;
MEMBER FUNCTION odciaggregateterminate (
SELF IN my_concat,
returnvalue OUT clob,
flags IN NUMBER
)
RETURN NUMBER
IS
BEGIN
returnvalue := curr_str;
RETURN odciconst.success;
END;
MEMBER FUNCTION odciaggregatemerge (
SELF IN OUT my_concat,
sctx2 IN my_concat
)
RETURN NUMBER
IS
BEGIN
IF (sctx2.curr_str IS NOT NULL)
THEN
SELF.curr_str := SELF.curr_str || ',' || sctx2.curr_str;
END IF;
RETURN odciconst.success;
END;
END;
/
CREATE OR REPLACE FUNCTION my_func_cat (p1 varchar2)
RETURN clob
AGGREGATE USING my_concat;
/

Related

PL/SQL Function return custom type exception

I have created a package which holds a custom type and a function that returns the custom type as below;
create or replace
PACKAGE INHOUSE_CUST_API
AS
TYPE doc_rec
IS
RECORD
(
doc_Title doc_issue_reference.title%Type,
doc_Number DOC_ISSUE_REFERENCE.DOC_NO%TYPE,
doc_Type DOC_ISSUE_REFERENCE.FILE_TYPE%TYPE,
doc_FileName DOC_ISSUE_REFERENCE.FILE_NAME%TYPE,
doc_Path DOC_ISSUE_REFERENCE.PATH%TYPE);
FUNCTION Get_Budget_Doc(
company IN VARCHAR2,
budget_process_id IN VARCHAR2,
budget_ptemplate_id IN VARCHAR2)
RETURN doc_rec;
END INHOUSE_CUST_API;
after that, I created the body of the function as below
create or replace
PACKAGE BODY INHOUSE_CUST_API
AS
FUNCTION Get_Budget_Doc(
company IN VARCHAR2,
budget_process_id IN VARCHAR2,
budget_ptemplate_id IN VARCHAR2)
RETURN doc_rec
IS
enhDocItem ENHANCED_DOC_REFERENCE_OBJECT%ROWTYPE;
docIssueRef DOC_ISSUE_REFERENCE%ROWTYPE;
docKeyValue VARCHAR2(150);
docIssueRef_rec doc_rec;
BEGIN
docKeyValue := company||'^'||budget_process_id||'^'||budget_ptemplate_id||'^';
--dbms_output.put_line(docKeyValue);
SELECT *
INTO enhDocItem
FROM ENHANCED_DOC_REFERENCE_OBJECT
WHERE KEY_VALUE= docKeyValue;
SELECT *
INTO docIssueRef
FROM DOC_ISSUE_REFERENCE
WHERE DOC_NO = enhDocItem.DOC_NO;
docIssueRef_rec.doc_Title :=docIssueRef.Title;
docIssueRef_rec.doc_Number:=docIssueRef.DOC_NO;
docIssueRef_rec.doc_Type :=docIssueRef.FILE_TYPE;
docIssueRef_rec.doc_Path :=docIssueRef.PATH;
RETURN docIssueRef_rec;
END Get_Budget_Doc;
END INHOUSE_CUST_API;
when I try to call the function as like
select INHOUSE_CUST_API.Get_Budget_Doc('param1','param2','param3') from dual;
I receive this exception
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
any help is appreciated.
You might want to use a table function to return your custom type. Here is a very simple example:
CREATE OR REPLACE PACKAGE brianl.deleteme AS
TYPE doc_rec_t IS RECORD
(
name VARCHAR2( 10 )
, age NUMBER( 3 )
);
TYPE doc_rec_tt IS TABLE OF doc_rec_t;
FUNCTION age( p_name IN VARCHAR2, p_age IN NUMBER, p_years IN INTEGER )
RETURN doc_rec_tt
PIPELINED;
END deleteme;
CREATE OR REPLACE PACKAGE BODY brianl.deleteme AS
FUNCTION age( p_name IN VARCHAR2, p_age IN NUMBER, p_years IN INTEGER )
RETURN doc_rec_tt
PIPELINED AS
l_ret doc_rec_t;
BEGIN
l_ret.name := p_name;
l_ret.age := p_age;
FOR i IN 1 .. p_years
LOOP
PIPE ROW (l_ret);
l_ret.age := l_ret.age + 1;
END LOOP;
END age;
END deleteme;
Invoke as follows:
SELECT * FROM TABLE( brianl.deleteme.age( 'Brian', 67, 3 ) );
The results:
NAME AGE
Brian 67
Brian 68
Brian 69
a SELECT statement in direct mode cann't return a complex data type like a record.

Correct Use of Oracle Type Member Functions

I have created user-defined types. It has member functions. Almost all use same package function report_pkg.get_data. And I do some other stuff.
create or replace type report_type is object
(
globalvar1 number(15),
globalvar2 varchar2(8),
member function getA return varchar2,
member function getB return varchar2,
member function getC return varchar2,
member function getD return varchar2
)
In member function, I use the same function.
member function getA return varchar2 is
rc tb_example%rowtype;
begin
rc := report_pkg.get_data(globalvar1 , globalvar2);
**Other stuff**
return rc.column1;
end;
member function getB return varchar2 is
rc tb_example%rowtype;
begin
rc := report_pkg.get_data(globalvar1 , globalvar2);
**Other stuff**
return rc.column2;
end;
member function getC return varchar2 is
rc tb_example%rowtype;
begin
rc := report_pkg.get_data(globalvar1 , globalvar2);
**Other stuff**
return rc.column3;
end;
member function getD return varchar2 is
rc tb_example%rowtype;
begin
rc := report_pkg.get_data(globalvar1 , globalvar2);
**Other stuff**
return rc.column4;
end;
When I run the below code in a loop;
report_tab := report_type(globalvar1, globalvar2);
insert into tb_report(columna, columnb, columnc, columnd)
values (report_tab.getA, report_tab.getB, report_tab.getC, report_tab.getD);
After all i run report_pkg.get_data for each member function. It brings performance issues out. How can i improve the performance and what is the correct use of user defined type member functions?
Thank you.

Calling a function within a procedure

I apologize ahead of time for the probably basic question. I am student and it is crunch time!
I am using Oracle 10g Express.
I created a function:
create or replace FUNCTION test_glaccounts_description
(
account_description_param VARCHAR2
)
RETURN NUMBER
AS
description_dup_var NUMBER;
BEGIN
SELECT 1
INTO description_dup_var
FROM general_ledger_accounts
WHERE account_description = account_description_param;
RETURN description_dup_var;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
END;
And would like to use that function in a procedure.
I've tried:
PROCEDURE insert_gla_with_test
(
account_number_param NUMBER,
account_description_param VARCHAR2
)
AS
BEGIN
IF test_glaccounts_description = 1 THEN
INSERT INTO general_ledger_accounts
VALUES (account_number_param, account_description_param);
ELSE raise_application_error (-20001, 'Duplicate account description');
END IF;
END;
But it doesn't like the "test_gla_accounts" line.....what am I doing wrong?
To my understanding, the function returns a value of 1 or 0, in the procedure, if the function returned a 1, I would like the param fields added to the table. If the function returned a 0, I would like the procedure to raise the error.
test_glaccounts_description takes a parameter (account_description_param) and returns a NUMBER. In order to call the function, therefore, you need to pass in a parameter. Assuming that you want to pass in the account_description_param that is passed in to the insert_gla_with_test procedure
CREATE OR REPLACE PROCEDURE insert_gla_with_test
(
account_number_param NUMBER,
account_description_param VARCHAR2
)
AS
BEGIN
IF test_glaccounts_description( account_description_param ) = 1 THEN
INSERT INTO general_ledger_accounts
VALUES (account_number_param, account_description_param);
ELSE
raise_application_error (-20001, 'Duplicate account description');
END IF;
END;

use User-Defined Aggregate Functions in package

my oracle version is 10.2.
I have a user_defined aggregate functions which using a object like this:
create type strcat_type as object (
cat_string varchar2(4000),
static function ODCIAggregateInitialize(cs_ctx In Out strcat_type) return number,
member function ODCIAggregateIterate(self In Out strcat_type,value in varchar2) return
number,
member function ODCIAggregateMerge(self In Out strcat_type,ctx2 In Out strcat_type)
return number,
member function ODCIAggregateTerminate(self In Out strcat_type,returnValue Out
varchar2,flags in number) return number
)
but when I try to put this object in a package,
create or replace package common is
type strcat_type as object (
cat_string varchar2(4000),
static function ODCIAggregateInitialize(cs_ctx In Out strcat_type) return number,
member function ODCIAggregateIterate(self In Out strcat_type,value in varchar2) return
number,
member function ODCIAggregateMerge(self In Out strcat_type,ctx2 In Out strcat_type)
return number,
member function ODCIAggregateTerminate(self In Out strcat_type,returnValue Out
varchar2,flags in number) return number
)
end common;
It causes pls-00540,pls-00707,how can I create a package with using this object?And how to put my type body and all those static and member functions in package body?
my type body like this:
create type body strcat_type is
static function ODCIAggregateInitialize(cs_ctx IN OUT strcat_type) return number
is
begin
cs_ctx := strcat_type( null );
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT strcat_type,
value IN varchar2 )
return number
is
begin
self.cat_string := self.cat_string || ','|| value;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN Out strcat_type,
returnValue OUT varchar2,
flags IN number)
return number
is
begin
returnValue := ltrim(rtrim(self.cat_string,','),',');
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT strcat_type,
ctx2 IN Out strcat_type)
return number
is
begin
self.cat_string := self.cat_string || ',' || ctx2.cat_string;
return ODCIConst.Success;
end;
end;
my function is like this:
CREATE or replace
FUNCTION strcat(input varchar2 )
RETURN varchar2
PARALLEL_ENABLE AGGREGATE USING strcat_type;
how to pack all these into a package and when I need aggregate,I just put 'common.strcat(colName)'?
OBJECT types are SQL objects (like tables, index...), they can't be defined in a package (just like you wouldn't define an index or a view inside a package).
See related SO question: Possible to create Oracle Database object types inside of PL/SQL?

How to call an Oracle PL/SQL object super method

I'd like to call an overridden PL/SQL method. Here's an example:
-- super class
create or replace type test as object
(
n number,
member procedure proc(SELF in out nocopy test, s varchar2)
)
alter type test not final
/
create or replace type body test is
member procedure proc(SELF in out nocopy test, s varchar2) is
begin
dbms_output.put_line('test1: n='||nvl(self.n, 'null')||' s='||s);
self.n := to_number(s);
end;
end;
/
-- derived class
create or replace type test2 under test
(
overriding member procedure proc(SELF in out nocopy test2, s varchar2)
)
/
Now I want to invoke the inherited version of the proc method. When I try to do an explicit cast like treat(self as test).proc(s); it won't compile because of PLS-00363: expression 'SYS_TREAT' cannot be used as an assignment target
The type body compiles when I use a local variable:
create or replace type body test2 is
overriding member procedure proc(SELF in out nocopy test2, s varchar2) is
O test;
begin
O := treat(self as test);
O.proc(s);
end;
end;
/
But when I run my example like this
declare
obj test2;
begin
obj := test2(0);
obj.proc('1');
end;
...it throws ORA-21780: Maximum number of object durations exceeded.
Is there any way to call test::proc (without serializing/deserializing)?
And... after proc has been called, how can any changed attributes (namely n) be reflected in obj ?
Update (Thanks, tbone):
I changed the organization of my methods using template methods ('before' and 'after'). I add them whenever I need to extend a method.
create or replace type test as object
(
n number,
member procedure proc (SELF in out nocopy test, s varchar2),
member procedure afterProc (SELF in out nocopy test, s varchar2)
member procedure beforeProc(SELF in out nocopy test, s varchar2),
)
not final
/
create or replace type body test is
member procedure proc(SELF in out nocopy test, s varchar2) is
begin
beforeProc(s);
dbms_output.put_line('test1: n='||nvl(n, 'null')||' s='||s);
n := to_number(s);
afterProc(s);
end;
member procedure afterProc (SELF in out nocopy test, s varchar2) is begin null; end;
member procedure beforeProc(SELF in out nocopy test, s varchar2) is begin null; end;
end;
/
To access the super methods, try either general invocation or generalized expression. For example, using a person supertype and student subtype:
CREATE OR REPLACE TYPE person_typ AS OBJECT (
idno number,
name varchar2(30),
phone varchar2(20),
MAP MEMBER FUNCTION get_idno RETURN NUMBER,
MEMBER FUNCTION show RETURN VARCHAR2)
NOT FINAL;
CREATE OR REPLACE TYPE BODY person_typ AS
MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
BEGIN
RETURN idno;
END;
MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
-- function that can be overriden by subtypes MEMBER FUNCTION show RETURN VARCHAR2 IS BEGIN
RETURN 'Id: ' || TO_CHAR(idno) || ', Name: ' || name;
END;
END;
CREATE TYPE student_typ UNDER person_typ (
dept_id NUMBER,
major VARCHAR2(30),
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2)
NOT FINAL;
CREATE TYPE BODY student_typ AS
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
RETURN (self AS person_typ).show || ' -- Major: ' || major ;
END;
END;
-- Using Generalized Invocation
DECLARE
myvar student_typ := student_typ(100, 'Sam', '6505556666', 100, 'Math');
name VARCHAR2(100);
BEGIN
name := (myvar AS person_typ).show; --Generalized invocation
END;
-- Using Generalized Expression
DECLARE
myvar2 student_typ := student_typ(101, 'Sam', '6505556666', 100, 'Math');
name2 VARCHAR2(100);
BEGIN
name2 := person_typ.show((myvar2 AS person_typ)); -- Generalized expression
END;
EDIT:
If you are on 10g, you'll need to organize the functions a bit different, but same functionality from the child to call the super method:
CREATE TYPE BODY person_typ AS
MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
BEGIN
RETURN idno;
END;
-- static function that can be called by subtypes
STATIC FUNCTION show_super (person_obj in person_typ) RETURN VARCHAR2 IS
BEGIN
RETURN 'Id: ' || TO_CHAR(person_obj.idno) || ', Name: ' || person_obj.name;
END;
-- function that can be overriden by subtypes
MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
RETURN person_typ.show_super ( SELF );
END;
END;
CREATE TYPE student_typ UNDER person_typ (
dept_id NUMBER,
major VARCHAR2(30),
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2)
NOT FINAL;
CREATE TYPE BODY student_typ AS
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
RETURN person_typ.show_super ( SELF ) || ' -- Major: ' || major ;
END;
END;
Now you'd call show_super() from student for the person method, or just show() for the student method.
From the docs, hope that helps.

Resources