Skip to main content

Importance of BULK COLLECT in Oracle PL/SQL #DOTC_MDB

 BULK COLLECT IN ORACLE AND IT’S ADVATAGE IN DATABASE

 

Hello Everyone,

In this blog we will discuss about BULK collect and its importance in Oracle Database. First we need to understand what is Bulk collect and how it’s improves the performance of a improving the speed of data retrieval.

Before going through this I suggest you to please go through my previous blogs related with PL/SQL block for better understanding.

BULK Collect: BULK COLLECT reduces context switches between SQL and PL/SQL engine and allows SQL engine to fetch the records at one time only. In simple word we can say BULK COLLECT SELECT statements that retrieve multiple rows with a single fetch, improving the speed of data retrieval. BULK COLLECT fetches the record in BULK, the INTO clause should always contain a collection type variable. The main advantage of using BULK COLLECT is it increases the performance by reducing the interaction between SQL and PL/SQL engine to improve the performance of our program.

SYNTAX OF BULK COLLECT

SELECT <column FROM TABLE> BULK COLLECT INTO bulk_varaible  FROM <table>;

FETCH <cursor> BULK COLLECT INTO <bulk_varaible >;

Syntax is similar to PL/SQL other block but here before INTO clause we specify BULK COLLECT to reduce context switches between SQL AND PL/SQL Engine.

Note: BULK COLLECT fetches the record in BULK, the INTO clause should always contain a collection type variable

For example let’s write one simple Bulk collect statement in PL/SQL to avoid context switching.

DECLARE

 TYPE ABC IS TABLE OF VARCHAR2(20);      -- COLLECTION NESTED TABLE

 V_NAME ABC;

BEGIN

    SELECT ENAME BULK COLLECT INTO V_NAME FROM EMP;

 FOR I IN 1..V_NAME.COUNT

LOOP

   DBMS_OUTPUT.PUT_LINE(I||' '||V_NAME(I));

END LOOP;

END;

/

Below is the output of above program , here we use collection type variable in BULK collect.

 


 

Another example of BULK Collect by using Cursor for the same program Using Different Table.

 

DECLARE CURSOR C1 IS

    SELECT EMPNAME FROM EMPLOYEE;

TYPE ABC IS TABLE OF VARCHAR2(20);

V_NAME ABC;

BEGIN

  OPEN C1;

   FETCH C1 BULK COLLECT INTO V_NAME;

   CLOSE C1;

  FOR I IN 1..V_NAME.COUNT

  LOOP

  DBMS_OUTPUT.PUT_LINE ('EMPLOYEE NAME IS ' ||' '||V_NAME(I));

   ​END LOOP;

​END;

/

 

Below is the output of above query:

 


Let’s understand LIMIT in Bulk Collect.

LIMIT :  LIMIT clause IN Oracle PL/SQL  restricts the number of rows fetched using BULK COLLECT with FETCH statement. In another word Oracle PL/SQL  has provided LIMIT clause that defines the number of records that needs to be included in the bulk.

Syntax of LIMIT in BULK COLLECT is as below :

FETCH <cursor_name> BULK COLLECT INTO <bulk_variable> LIMIT <size>;

Below is the program of BULK COLLECT using LIMIT clause .

 

DECLARE CURSOR C1 IS

    SELECT EMPNAME FROM EMPLOYEE WHERE DEPTNO IN (30,40,20);

TYPE ABC IS TABLE OF VARCHAR2(20);

V_NAME ABC;

BEGIN

  OPEN C1;

   FETCH C1 BULK COLLECT INTO V_NAME LIMIT 10;

   CLOSE C1;

  FOR I IN 1..V_NAME.COUNT

  LOOP

  DBMS_OUTPUT.PUT_LINE ('EMPLOYEE NAME IS ' ||' '||V_NAME(I));

   ​END LOOP;

​END;

/

 

Output of above program is as below:

 


 

FOR ALL STATEMENT: FORALL allows to perform the DML operations on data in bulk. It is similar to that of FOR loop statement except in FOR loop things happen at the record-level whereas in FORALL there is no LOOP concept. Instead the entire data present in the given range is processed at the same time. FOR ALL STATEMENT INSERTs, UPDATEs, and DELETEs that use collections to change multiple rows of data very quickly. No need to use loop for retrieving and performing multiple records.

Syntax of FOR ALL STATEMENT

FORALL <loop_variable>in<lower range> .. <higher range>

 

<DML operations>;

 

Below is the program by using FOR ALL STATEMENT.

DECLARE

   CURSOR C1 IS SELECT EMPNAME FROM EMPLOYEE;

   TYPE ABC IS TABLE OF VARCHAR2(100);

   V_NAME ABC;

   BEGIN

   OPEN C1;

    FETCH C1 BULK COLLECT INTO  V_NAME LIMIT 12;

    FOR I IN V_NAME.FIRST .. V_NAME.LAST

LOOP

   DBMS_OUTPUT.PUT_LINE('NAME IS ' ||I);

   END LOOP;

   FORALL J IN V_NAME.FIRST .. V_NAME.LAST

    UPDATE EMPLOYEE SET DEPTNO=40 WHERE EMPNAME=V_NAME(J);

   DBMS_OUTPUT.PUT_LINE('DEPTNO IS UPDATED TO 40');

    CLOSE C1;

  END;

/

 

Below is the output of above program.

 

 


 

 

BULK COLLECT Attributes : ATTRIBUTES are similar to cursor attributes BULK COLLECT has %BULK_ROWCOUNT(n) that returns the number of rows affected in the nth DML statement of the FORALL statement.

Advantage of BULK COLLECT and FOR ALL STATEMENT in ORACLE PL/SQL.

Every program PL/SQL developers write includes both PL/SQL and SQL statements. PL/SQL statements are run by the PL/SQL statement executor and SQL statements are run by the SQL statement executor. When the PL/SQL runtime engine encounters a SQL statement, it stops and passes the SQL statement over to the SQL engine. It uses Context Switches and Performance of the query .

 

Writing one program to see the performance of both using FOR LOOP and FOR ALL STATEMENT and see the execution time of both and see the performance .

Below is the program.

 

DECLARE

  TYPE ABC IS TABLE OF TEST.NUM%TYPE INDEX BY PLS_INTEGER;

  TYPE BCD IS TABLE OF TEST1.NAME%TYPE INDEX BY PLS_INTEGER;

   V_NUM  ABC;

   V_NAME BCD;

  ITERATIONS CONSTANT PLS_INTEGER := 500;

  T1 NUMBER;

  T2 NUMBER;

  T3 NUMBER;

BEGIN

  FOR J IN 1..ITERATIONS

LOOP 

     V_NUM(J) := J;

     V_NAME(J) := 'Part No. ' || TO_CHAR(J);

  END LOOP;

  T1 := DBMS_UTILITY.GET_TIME;

  FOR I IN 1..iterations LOOP 

     INSERT INTO TEST VALUES (V_NUM(I), V_NAME(I));

  END LOOP;

  T2 := DBMS_UTILITY.GET_TIME;

  FORALL i IN 1..iterations 

     INSERT INTO TEST1 VALUES (V_NUM(I), V_NAME(I));

  T3 := DBMS_UTILITY.GET_TIME;

  DBMS_OUTPUT.PUT_LINE('EXECUTION  IN SECOND');

  DBMS_OUTPUT.PUT_LINE('---------------------');

  DBMS_OUTPUT.PUT_LINE('FOR LOOP EXECUTION: ' || TO_CHAR((T2 - T1)/100));

  DBMS_OUTPUT.PUT_LINE('FORALL EXECUTION:   ' || TO_CHAR((T3 - T2)/100));

  COMMIT;

END;

/

 

Output of above program is as below.

 


 

Please go through this blog as BULK COLLECT and FOR ALL STATEMENT Play important role in Performance tuning . This blog is important for Fresher’s as well as experienced professional who is preparing for interview  as lots of questions asked from this topic. Also try to use Bulk collect in Sub- program and in Dynamic SQL. Please let me know if you have any questions.

Thanks.

 

Comments

  1. An online database of training for customers

    Using customer databases, businesses keep track of all their current and potential customers. Data stored in the customer database includes personal details, buying habits, contact information, etc. Firms use such databases to understand the buying patterns of their customers, which are used to design products and set prices. Businesses use the available list of potential customers to boost sales. Also, such a customer database makes it possible for the firm to provide personalized service to the customers.online Database of training for customers



    The purpose of a customer database is to organize and analyze customer data that is used to create a marketing plan for a specific product or service. Current or past data gathered in the customer database from different sources like electronic transactions and web activity is stored in structured form in databases which aid in efficient decision makingonline Customer training database



    ReplyDelete
  2. I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.

    Data Engineering Services 

    AI & ML Services

    Data Analytics Services

    Data Modernization Services

    ReplyDelete
  3. Hey,
    I’ve read your full post, the post is great to enhance our knowledge about ReactJS. I’m damn sure you will keep it up to take it on next level in near future.
    We’ve my own blogs about ReactJS, just take give some time to visit. Moreover, having a variety developers too. Just visit given links. Thanks
    Iyrix Technologies
    Remote Software Developers
    Software Development Services
    UI/UX Designers
    Check Developers Rates
    Website: ReactJS Stories
    7 Reasons why choose ReactJS
    10 Reasons To Use ReactJS For Enterprise App Development
    Business Benefits of ReactJS Framework for Modern Web and App Development
    Top React Static Site Generators for 2022

    ReplyDelete

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you que...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...