oracle-databaseplsqlplsqldeveloperoracle11gr2

Setting a value for LIMIT while using bulk collect


I wanted to know if we have any technique by which we can calculate the value which needed to be set for a LIMIT clause of bulk collect operation. For example below, lets say our cursor has 10 Million records..What is the value which we can set for LIMIT clause to have optimum performance. Is there any way we can calculate it.

decalre
cursor c_emp is <some select query>

var  <variable> ;

begin
     open c_emp;
       loop
           fetch c_emp bulk collect into var limit 2;
           exit when c_emp%NOTFOUND;
      end loop;
     close c_emp;
  end;

Solution

  • Use an implicit cursor in a cursor FOR LOOP. It makes the code simpler and the default value of 100 is almost always good enough.

    I've seen a lot of people waste a lot of time worrying about this. If you think about why bulk collect improves performance you will understand why large numbers won't help.

    Bulk collect improves performance by reducing the context switches between SQL and PL/SQL. Imagine the highly-unlikely worst case scenario, where context switching uses up all the run time. A limit of 2 eliminates 50% of the context switches; 10 eliminates 90%; 100 eliminates 99%, etc. Plot it out and you'll realize it's not worth finding the optimal limit size:

    enter image description here

    This 1/N harmonic progression performance behavior can be found in other Oracle features, such as sequence caching and application row fetching.

    Use the defaults. Spend your time worrying about more important things.