Performance Tuning In Plsql Function Query












1















I have plsql function. And this function has query like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ( select filter_coloumn_name from table_name_2);


My function is used by other query to return value from query. This query which use this function take more time. Even the site (front screen) down. Because table_name_2 has three million record. So that i must use some performance tuning method in my function. I change my function query like as below:



cursor my_cursor IS  
select filter_coloumn_name from table_name_2;

TYPE cursor_array_type IS TABLE OF my_cursor%ROWTYPE INDEX BY BINARY_INTEGER;
m cursor_array_type;

TYPE cursor_table_type IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
cursor_table_object cursor_table_type;

fetch_size NUMBER := 5000;
index_var number;

begin
index_var := 1;

open my_cursor;
loop
FETCH my_cursor BULK COLLECT
into m LIMIT fetch_size;
exit when my_cursor %notfound;

for i in 1 .. m.count loop
cursor_table_object (index_var) := m(i).filter_coloumn_name;
index_var := index_var + 1;
end loop;
end loop;
Close my_cursor;

select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);



select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);




Namely, i want fetch all values at one time, and then i want use this table object like as above. But i can't use table object with in condition expression in sql query.



I take PLS-00382: expression is of wrong type error.



I want to use this table object like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ('bla', 'bla bla', 'bla bla bla');


Should i convert table object to the array ?










share|improve this question

























  • How much record in table_name table ?

    – Muhammad Azim
    Jan 1 at 18:04











  • it has three million record too.

    – Mert Özoğul
    Jan 1 at 18:06











  • Do you need to return all the record at once ? Could you please explain the purpose of this function ? Because returning 3 million record is very high.

    – Muhammad Azim
    Jan 1 at 18:13











  • It has some condition so record count will be reduced. But i must use ready, fetched data (array, table) after in condition.

    – Mert Özoğul
    Jan 1 at 18:17











  • Instead of function use View and use inner join instead of sub query. Please share the condition information

    – Muhammad Azim
    Jan 1 at 18:19


















1















I have plsql function. And this function has query like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ( select filter_coloumn_name from table_name_2);


My function is used by other query to return value from query. This query which use this function take more time. Even the site (front screen) down. Because table_name_2 has three million record. So that i must use some performance tuning method in my function. I change my function query like as below:



cursor my_cursor IS  
select filter_coloumn_name from table_name_2;

TYPE cursor_array_type IS TABLE OF my_cursor%ROWTYPE INDEX BY BINARY_INTEGER;
m cursor_array_type;

TYPE cursor_table_type IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
cursor_table_object cursor_table_type;

fetch_size NUMBER := 5000;
index_var number;

begin
index_var := 1;

open my_cursor;
loop
FETCH my_cursor BULK COLLECT
into m LIMIT fetch_size;
exit when my_cursor %notfound;

for i in 1 .. m.count loop
cursor_table_object (index_var) := m(i).filter_coloumn_name;
index_var := index_var + 1;
end loop;
end loop;
Close my_cursor;

select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);



select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);




Namely, i want fetch all values at one time, and then i want use this table object like as above. But i can't use table object with in condition expression in sql query.



I take PLS-00382: expression is of wrong type error.



I want to use this table object like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ('bla', 'bla bla', 'bla bla bla');


Should i convert table object to the array ?










share|improve this question

























  • How much record in table_name table ?

    – Muhammad Azim
    Jan 1 at 18:04











  • it has three million record too.

    – Mert Özoğul
    Jan 1 at 18:06











  • Do you need to return all the record at once ? Could you please explain the purpose of this function ? Because returning 3 million record is very high.

    – Muhammad Azim
    Jan 1 at 18:13











  • It has some condition so record count will be reduced. But i must use ready, fetched data (array, table) after in condition.

    – Mert Özoğul
    Jan 1 at 18:17











  • Instead of function use View and use inner join instead of sub query. Please share the condition information

    – Muhammad Azim
    Jan 1 at 18:19
















1












1








1








I have plsql function. And this function has query like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ( select filter_coloumn_name from table_name_2);


My function is used by other query to return value from query. This query which use this function take more time. Even the site (front screen) down. Because table_name_2 has three million record. So that i must use some performance tuning method in my function. I change my function query like as below:



cursor my_cursor IS  
select filter_coloumn_name from table_name_2;

TYPE cursor_array_type IS TABLE OF my_cursor%ROWTYPE INDEX BY BINARY_INTEGER;
m cursor_array_type;

TYPE cursor_table_type IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
cursor_table_object cursor_table_type;

fetch_size NUMBER := 5000;
index_var number;

begin
index_var := 1;

open my_cursor;
loop
FETCH my_cursor BULK COLLECT
into m LIMIT fetch_size;
exit when my_cursor %notfound;

for i in 1 .. m.count loop
cursor_table_object (index_var) := m(i).filter_coloumn_name;
index_var := index_var + 1;
end loop;
end loop;
Close my_cursor;

select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);



select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);




Namely, i want fetch all values at one time, and then i want use this table object like as above. But i can't use table object with in condition expression in sql query.



I take PLS-00382: expression is of wrong type error.



I want to use this table object like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ('bla', 'bla bla', 'bla bla bla');


Should i convert table object to the array ?










share|improve this question
















I have plsql function. And this function has query like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ( select filter_coloumn_name from table_name_2);


My function is used by other query to return value from query. This query which use this function take more time. Even the site (front screen) down. Because table_name_2 has three million record. So that i must use some performance tuning method in my function. I change my function query like as below:



cursor my_cursor IS  
select filter_coloumn_name from table_name_2;

TYPE cursor_array_type IS TABLE OF my_cursor%ROWTYPE INDEX BY BINARY_INTEGER;
m cursor_array_type;

TYPE cursor_table_type IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
cursor_table_object cursor_table_type;

fetch_size NUMBER := 5000;
index_var number;

begin
index_var := 1;

open my_cursor;
loop
FETCH my_cursor BULK COLLECT
into m LIMIT fetch_size;
exit when my_cursor %notfound;

for i in 1 .. m.count loop
cursor_table_object (index_var) := m(i).filter_coloumn_name;
index_var := index_var + 1;
end loop;
end loop;
Close my_cursor;

select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);



select colum_name
from table_name
where filter_coloumn_name in (cursor_table_object);




Namely, i want fetch all values at one time, and then i want use this table object like as above. But i can't use table object with in condition expression in sql query.



I take PLS-00382: expression is of wrong type error.



I want to use this table object like as below:



select  colum_name 
from table_name
where filter_coloumn_name in ('bla', 'bla bla', 'bla bla bla');


Should i convert table object to the array ?







sql oracle plsql query-performance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 17:56









Barbaros Özhan

13.7k71633




13.7k71633










asked Jan 1 at 17:43









Mert ÖzoğulMert Özoğul

146110




146110













  • How much record in table_name table ?

    – Muhammad Azim
    Jan 1 at 18:04











  • it has three million record too.

    – Mert Özoğul
    Jan 1 at 18:06











  • Do you need to return all the record at once ? Could you please explain the purpose of this function ? Because returning 3 million record is very high.

    – Muhammad Azim
    Jan 1 at 18:13











  • It has some condition so record count will be reduced. But i must use ready, fetched data (array, table) after in condition.

    – Mert Özoğul
    Jan 1 at 18:17











  • Instead of function use View and use inner join instead of sub query. Please share the condition information

    – Muhammad Azim
    Jan 1 at 18:19





















  • How much record in table_name table ?

    – Muhammad Azim
    Jan 1 at 18:04











  • it has three million record too.

    – Mert Özoğul
    Jan 1 at 18:06











  • Do you need to return all the record at once ? Could you please explain the purpose of this function ? Because returning 3 million record is very high.

    – Muhammad Azim
    Jan 1 at 18:13











  • It has some condition so record count will be reduced. But i must use ready, fetched data (array, table) after in condition.

    – Mert Özoğul
    Jan 1 at 18:17











  • Instead of function use View and use inner join instead of sub query. Please share the condition information

    – Muhammad Azim
    Jan 1 at 18:19



















How much record in table_name table ?

– Muhammad Azim
Jan 1 at 18:04





How much record in table_name table ?

– Muhammad Azim
Jan 1 at 18:04













it has three million record too.

– Mert Özoğul
Jan 1 at 18:06





it has three million record too.

– Mert Özoğul
Jan 1 at 18:06













Do you need to return all the record at once ? Could you please explain the purpose of this function ? Because returning 3 million record is very high.

– Muhammad Azim
Jan 1 at 18:13





Do you need to return all the record at once ? Could you please explain the purpose of this function ? Because returning 3 million record is very high.

– Muhammad Azim
Jan 1 at 18:13













It has some condition so record count will be reduced. But i must use ready, fetched data (array, table) after in condition.

– Mert Özoğul
Jan 1 at 18:17





It has some condition so record count will be reduced. But i must use ready, fetched data (array, table) after in condition.

– Mert Özoğul
Jan 1 at 18:17













Instead of function use View and use inner join instead of sub query. Please share the condition information

– Muhammad Azim
Jan 1 at 18:19







Instead of function use View and use inner join instead of sub query. Please share the condition information

– Muhammad Azim
Jan 1 at 18:19














2 Answers
2






active

oldest

votes


















1














A suggestion, based on what you've said so far: how about using a table function? It makes it possible to use something like statement you wanted to use, i.e.



select colum_name from table_name where filter_coloumn_name in (cursor_table_object);


but which returns




PLS-00382: expression is of wrong type






Here's an example based on Scott's schema. Have a look:



SQL> create or replace function f_test
2 return sys.odcinumberlist
3 is
4 -- Cursor's SELECT should contain your 3-million-rows table
5 cursor cur_r is select deptno from dept where deptno < 30;
6 l_ret sys.odcinumberlist;
7 begin
8 open cur_r;
9 fetch cur_r bulk collect into l_ret;
10 close cur_r;
11 return l_ret;
12 end;
13 /

Function created.

SQL> -- How to use it?
SQL> select e.ename
2 from emp e join table(f_test) x on x.column_value = e.deptno;

ENAME
----------
MILLER
KING
CLARK
ADAMS
SCOTT
FORD
JONES
SMITH

8 rows selected.

SQL>





share|improve this answer
























  • Can you elaborate on the 'sys.odcinumberlist' type ?

    – Mert Özoğul
    Jan 1 at 19:36











  • It is built-in collection type for numeric values (so that I wouldn't have to declare my own type for this purpose).

    – Littlefoot
    Jan 1 at 19:41











  • thanks Littlefoot.

    – Mert Özoğul
    Jan 1 at 19:44











  • You're welcome.

    – Littlefoot
    Jan 1 at 19:56



















0














You must use exists instead of in, also table_name_2 must have index over filter_coloumn_name field.
Using cursor only what you get is full access to a big table.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997588%2fperformance-tuning-in-plsql-function-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    A suggestion, based on what you've said so far: how about using a table function? It makes it possible to use something like statement you wanted to use, i.e.



    select colum_name from table_name where filter_coloumn_name in (cursor_table_object);


    but which returns




    PLS-00382: expression is of wrong type






    Here's an example based on Scott's schema. Have a look:



    SQL> create or replace function f_test
    2 return sys.odcinumberlist
    3 is
    4 -- Cursor's SELECT should contain your 3-million-rows table
    5 cursor cur_r is select deptno from dept where deptno < 30;
    6 l_ret sys.odcinumberlist;
    7 begin
    8 open cur_r;
    9 fetch cur_r bulk collect into l_ret;
    10 close cur_r;
    11 return l_ret;
    12 end;
    13 /

    Function created.

    SQL> -- How to use it?
    SQL> select e.ename
    2 from emp e join table(f_test) x on x.column_value = e.deptno;

    ENAME
    ----------
    MILLER
    KING
    CLARK
    ADAMS
    SCOTT
    FORD
    JONES
    SMITH

    8 rows selected.

    SQL>





    share|improve this answer
























    • Can you elaborate on the 'sys.odcinumberlist' type ?

      – Mert Özoğul
      Jan 1 at 19:36











    • It is built-in collection type for numeric values (so that I wouldn't have to declare my own type for this purpose).

      – Littlefoot
      Jan 1 at 19:41











    • thanks Littlefoot.

      – Mert Özoğul
      Jan 1 at 19:44











    • You're welcome.

      – Littlefoot
      Jan 1 at 19:56
















    1














    A suggestion, based on what you've said so far: how about using a table function? It makes it possible to use something like statement you wanted to use, i.e.



    select colum_name from table_name where filter_coloumn_name in (cursor_table_object);


    but which returns




    PLS-00382: expression is of wrong type






    Here's an example based on Scott's schema. Have a look:



    SQL> create or replace function f_test
    2 return sys.odcinumberlist
    3 is
    4 -- Cursor's SELECT should contain your 3-million-rows table
    5 cursor cur_r is select deptno from dept where deptno < 30;
    6 l_ret sys.odcinumberlist;
    7 begin
    8 open cur_r;
    9 fetch cur_r bulk collect into l_ret;
    10 close cur_r;
    11 return l_ret;
    12 end;
    13 /

    Function created.

    SQL> -- How to use it?
    SQL> select e.ename
    2 from emp e join table(f_test) x on x.column_value = e.deptno;

    ENAME
    ----------
    MILLER
    KING
    CLARK
    ADAMS
    SCOTT
    FORD
    JONES
    SMITH

    8 rows selected.

    SQL>





    share|improve this answer
























    • Can you elaborate on the 'sys.odcinumberlist' type ?

      – Mert Özoğul
      Jan 1 at 19:36











    • It is built-in collection type for numeric values (so that I wouldn't have to declare my own type for this purpose).

      – Littlefoot
      Jan 1 at 19:41











    • thanks Littlefoot.

      – Mert Özoğul
      Jan 1 at 19:44











    • You're welcome.

      – Littlefoot
      Jan 1 at 19:56














    1












    1








    1







    A suggestion, based on what you've said so far: how about using a table function? It makes it possible to use something like statement you wanted to use, i.e.



    select colum_name from table_name where filter_coloumn_name in (cursor_table_object);


    but which returns




    PLS-00382: expression is of wrong type






    Here's an example based on Scott's schema. Have a look:



    SQL> create or replace function f_test
    2 return sys.odcinumberlist
    3 is
    4 -- Cursor's SELECT should contain your 3-million-rows table
    5 cursor cur_r is select deptno from dept where deptno < 30;
    6 l_ret sys.odcinumberlist;
    7 begin
    8 open cur_r;
    9 fetch cur_r bulk collect into l_ret;
    10 close cur_r;
    11 return l_ret;
    12 end;
    13 /

    Function created.

    SQL> -- How to use it?
    SQL> select e.ename
    2 from emp e join table(f_test) x on x.column_value = e.deptno;

    ENAME
    ----------
    MILLER
    KING
    CLARK
    ADAMS
    SCOTT
    FORD
    JONES
    SMITH

    8 rows selected.

    SQL>





    share|improve this answer













    A suggestion, based on what you've said so far: how about using a table function? It makes it possible to use something like statement you wanted to use, i.e.



    select colum_name from table_name where filter_coloumn_name in (cursor_table_object);


    but which returns




    PLS-00382: expression is of wrong type






    Here's an example based on Scott's schema. Have a look:



    SQL> create or replace function f_test
    2 return sys.odcinumberlist
    3 is
    4 -- Cursor's SELECT should contain your 3-million-rows table
    5 cursor cur_r is select deptno from dept where deptno < 30;
    6 l_ret sys.odcinumberlist;
    7 begin
    8 open cur_r;
    9 fetch cur_r bulk collect into l_ret;
    10 close cur_r;
    11 return l_ret;
    12 end;
    13 /

    Function created.

    SQL> -- How to use it?
    SQL> select e.ename
    2 from emp e join table(f_test) x on x.column_value = e.deptno;

    ENAME
    ----------
    MILLER
    KING
    CLARK
    ADAMS
    SCOTT
    FORD
    JONES
    SMITH

    8 rows selected.

    SQL>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 1 at 19:11









    LittlefootLittlefoot

    23.3k71533




    23.3k71533













    • Can you elaborate on the 'sys.odcinumberlist' type ?

      – Mert Özoğul
      Jan 1 at 19:36











    • It is built-in collection type for numeric values (so that I wouldn't have to declare my own type for this purpose).

      – Littlefoot
      Jan 1 at 19:41











    • thanks Littlefoot.

      – Mert Özoğul
      Jan 1 at 19:44











    • You're welcome.

      – Littlefoot
      Jan 1 at 19:56



















    • Can you elaborate on the 'sys.odcinumberlist' type ?

      – Mert Özoğul
      Jan 1 at 19:36











    • It is built-in collection type for numeric values (so that I wouldn't have to declare my own type for this purpose).

      – Littlefoot
      Jan 1 at 19:41











    • thanks Littlefoot.

      – Mert Özoğul
      Jan 1 at 19:44











    • You're welcome.

      – Littlefoot
      Jan 1 at 19:56

















    Can you elaborate on the 'sys.odcinumberlist' type ?

    – Mert Özoğul
    Jan 1 at 19:36





    Can you elaborate on the 'sys.odcinumberlist' type ?

    – Mert Özoğul
    Jan 1 at 19:36













    It is built-in collection type for numeric values (so that I wouldn't have to declare my own type for this purpose).

    – Littlefoot
    Jan 1 at 19:41





    It is built-in collection type for numeric values (so that I wouldn't have to declare my own type for this purpose).

    – Littlefoot
    Jan 1 at 19:41













    thanks Littlefoot.

    – Mert Özoğul
    Jan 1 at 19:44





    thanks Littlefoot.

    – Mert Özoğul
    Jan 1 at 19:44













    You're welcome.

    – Littlefoot
    Jan 1 at 19:56





    You're welcome.

    – Littlefoot
    Jan 1 at 19:56













    0














    You must use exists instead of in, also table_name_2 must have index over filter_coloumn_name field.
    Using cursor only what you get is full access to a big table.






    share|improve this answer




























      0














      You must use exists instead of in, also table_name_2 must have index over filter_coloumn_name field.
      Using cursor only what you get is full access to a big table.






      share|improve this answer


























        0












        0








        0







        You must use exists instead of in, also table_name_2 must have index over filter_coloumn_name field.
        Using cursor only what you get is full access to a big table.






        share|improve this answer













        You must use exists instead of in, also table_name_2 must have index over filter_coloumn_name field.
        Using cursor only what you get is full access to a big table.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 17:50









        lugolulugolu

        4615




        4615






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997588%2fperformance-tuning-in-plsql-function-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas