Skip to main content

bulk bind

 bulk collection

when you delete a set of records that you wish to using a pl/sql program , 

oracle takes one empno number from pl/sql engine at a time to sql engine and executes it and comes back for the next one.


If you have 100 such empno-records then oracle makes 100 such trips to pl/sql and sql engines.

this trip between sql engine and pl/sql engine is called CONTEXT SWITCH.

To ensure that only one context switch is made and not 100 or more we do the following:

bulk bind the entire collection by using "FORALL syntax" to complete the job at one go.

when oracle reads FORALL it understands that the entire collection of values are to be passed to the pl/sql engine for execution and not 100 context switches.


Although the FORALL statement contains an  iteration scheme, it is not a FOR loop.



wap to read all the records from emp table and load them to a new table
emp_dup_tab, using bulk bind concept .



create table emp_dup_table as select * from emp where 1=2;


declare
type dept_tab_type is table of dept%rowtype;
my_dept dept_tab_type;
begin
select * bulk collect into my_dept from dept;
for indx in my_dept.first..my_dept.last loop
 dbms_output.put_line(  my_dept(indx).deptno||'  '||my_dept(indx).dname||'  '||my_dept(indx).loc);
 end loop;
end;
/

---------------------------------------

create table emp_dup_table as select * from emp where 1=2;

/
select count(*) from emp_dup_table;

create or replace procedure fast_proc is
         type emp_table_type is table of emp%ROWTYPE;
         emp_table emp_table_type;
         begin
         select  * BULK COLLECT INTO emp_table from emp;

         forall i in emp_table.First..emp_table.Last
       insert into emp_dup_table values emp_table(i) ;
       end fast_proc;
 /

exec emp_dup_table;

select count(*) from emp_dup_table;


One method of fetching data is an Oracle bulk collect. With Oracle bulk collect, the PL/SQL engine tells the SQL engine to collect many rows at once and place them in a collection.
During an Oracle bulk collect, the SQL engine retrieves all the rows and loads them into the collection and switches back to the PL/SQL engine.  When rows are retrieved using Oracle bulk collect, they are retrieved with only two context switches.  The larger the number of rows to collect  the better the performance you will see using an Oracle bulk collect.

Comments

Unknown said…
Nice explanation with a good example.
Thank you, Sir
ANURAG said…
Nice explanation .
jathin said…
Good explanation with example.
nikhil said…
Great explanation!!!
Lakshman said…
This is clear, concise, and complete!
Venu Gopal said…
Clear and good description of the topic
Hemanth said…
Good explanation it is helpful to understand easily to a beginner level learner.
Prem Reddy said…
Good Explanation with examples, from creating tables to advance PL/SQL Thank you sir for these valuable resources
nikhil said…
This comment has been removed by the author.
Nagireddy said…
Nice explanation, The practical examples and hands-on exercises have significantly enhanced my understanding
Swetha said…
Good Explanation. Examples are easy to understand that how actually the concept works
Thank you so much sir, for such a crystal clear explanation with example. Your way of teaching makes complex topics simple
to understand.
LEKHA said…
Your examples makes the topic much clearer. I appreciate how you tied everything back to real-world applications, which helped me see the practical relevance of these database concepts. I liked how you broke down complex topic into simple steps. "Keep up the great work!".

Popular posts from this blog

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C   NUMBER(3); BEGIN WHILE B <

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7     1.      Introduction This document outlines a mini project for the RDBMS LOT

sample tables

  --sample tables DROP TABLE EMP; DROP TABLE DEPT; DROP TABLE BONUS; DROP TABLE SALGRADE; DROP TABLE DUMMY;   CREATE TABLE EMP        (EMPNO NUMBER(4) NOT NULL,         ENAME VARCHAR2(10),         JOB VARCHAR2(9),         MGR NUMBER(4),         HIREDATE DATE,         SAL NUMBER(7, 2),         COMM NUMBER(7, 2),         DEPTNO NUMBER(2));   INSERT INTO EMP VALUES         (7369, 'SMITH',   'CLERK',      7902,         TO_DATE('17-DEC-1980', 'DD-MON-YYYY'),   800, NULL, 20); INSERT INTO EMP VALUES         (7499, 'ALLEN',   'SALESMAN',   7698,         TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600,   300, 30); INSERT INTO EMP VALUES         (7521, 'WARD',    'SALESMAN',   7698,         TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250,   500, 30); INSERT INTO EMP VALUES         (7566, 'JONES',   'MANAGER',    7839,         TO_DATE('2-APR-1981', 'DD-MON-YYYY'),   2975, NULL,