Speed-up your Inserts

Problem Statement:

  1. Load millions of rows from flat files (csv) to database
  2. Load one table from another huge table.
  3. Speeding Insert statements

Approach:

For creating record in database using insert there are two methods.  One is conventional and the other is direct path.  If we look at the performance aspects of both the approach, latter is much faster than the earlier one.

Few of the reasons are:

  1. Direct path does not write on partially written blocks, so no need to find them.  This removes overhead of finding blocks suitable for writing.  It always writes above the high water mark.  This also removes overhead of partially written block into memory for new record creation in those blocks.
  2. In oppose to conventional path, where load calls oracle to lock and unlock table and indexes for each set (array) of records it processed, direct path calls oracle to lock table and indexes at the beginning and unlock at end.  This reduces the overhead of locking and unlocking during load.
  3. Instead of using oracle’s buffer cache, direct path read performs their own write I/O.  This not only minimizes the contentions with other users but also improve performances of inserts.
  4. Direct Path always bypass UNDO.
  5. Indexes on tables for which direct path insert happens, it builds mini indexes separately and merge them into actual index in bulk.  This operation is much –much faster.

A direct path insert statement can be written as:

  1. CREATE TABLE .. AS SELECT
  2. INSERT /*+ APPEND */ INTO … SELECT ..
  3. INSERT /*+ APPEND_VALUES */ INTO .. VALUES ..

The hint provided in the statement is the way to tell oracle to make your insert statement parallel and direct path.  These hints are based on the type of statement used.  APPEND hint should be used with the statement of type “INSERT INTO .. SELECT” and APPEND_VALUES should be used with the statement of type “INSERT INTO .. VALUES ..”.

Let’s understand this with the help of an example:

Prepare for Performance Meaasure:

  • Create a simple table DPATHINSERT [with only id as number and name as varchar2(100)] and Gather its stats.  Before starting validate the HWM allocated for this table and physical direct path write stats.

  • Let’s see how many blocks below the HWM allocated to the table

  • Let’s see how many physical direct write operations we have currently performed:

Conventional Insert:

  • Insert records in the table with conventional insert statement and validate HWM and Direct Path Write.  There are no direct path write found after operation for conventional insert.

Direct Path Insert:

  • Insert records in the table with Direct path insert statement and validate HWM and Direct Path Write. You will find one direct path write and block count to 9 from previous block count 5.

Performance Comparison:

There are substantial improvement in insert performance using direct path as compared to conventional path insert.  We have compared numbers between these two type of inserts for same set of records.

Tried to insert 3 Million (3000000) records in the same table multiple times and found different times taken for each insert.  Maximum time it took was 60 seconds while minimum it took was 21 seconds.

While with Direct path insert, the time was nearly constant and found to be 9 seconds which was a substantial improvement over 21 Seconds of conventional insert time.

Leave a Reply

Your email address will not be published. Required fields are marked *