Published: 31 Oct 2007
By: Chinh Do

Chinh Do describes techniques to improve application performance with bulk operations using ODP.NET.

Introduction

In a typical multi-tier application, one of the biggest performance bottlenecks is the overhead of making round-trips to the database. Minimizing these round-trips is often the first area you should look at during performance tuning. Fortunately, the Oracle Data Provider for .NET (ODP.NET) makes it fairly easy to do this by providing several built-in methods to write and read data in bulk.

To run the code samples in this article, you will need to have:

  • ODP.NET 2.0 (download from here).
  • A table named "BULK_TEST".

Here's the script that creates the table BULK_TEST:

Bulk Inserts Using Array Binding

The Array Binding feature in ODP.NET allows you to insert multiple records in one database call. To use Array Binding, you simply set OracleCommand.ArrayBindCount to the number of records to be inserted, and pass arrays of values as parameters instead of single values:

As you can see, the code does not look that much different from doing a regular single-record insert. However, the performance improvement is quite drastic, depending on the number of records involved. The more records you have to insert, the bigger the performance gain. On my development PC, inserting 1,000 records using Array Binding is 90 times faster than inserting the records one at a time. Yes, you read that right: 90 times faster! Your results will vary, depending on the record size and network speed/bandwidth to the database server.

A bit of investigative work reveals that the SQL is considered to be "executed" multiple times on the server side. The evidence comes from V$SQL (look at the EXECUTIONS column). However, from the .NET point of view, everything was done in one call.

Bulk Inserts Using PL/SQL Associative Arrays

PL/SQL Associative Arrays (formerly PL/SQL Index-By Tables) allow .NET code to pass arrays as parameters to PL/SQL code (stored procedure or anonymous PL/SQL blocks). Once the arrays are in PL/SQL, you are free to use them in whichever way you wish, including turning around and inserting them into a table using the "forall" bulk bind syntax.

Why would you want to insert bulk records using PL/SQL Associative Arrays instead of the simple syntax of Array Binding? Here are a few possible reasons:

  • You need to perform additional work in PL/SQL, in addition to the bulk insert.
  • The application login does not have permission to perform the work, but you can grant the necessary privilege to a stored procedure.

The major drawback with using Associative Arrays is that you have to write PL/SQL code. I have nothing against PL/SQL, but it's not part of the skill set of the typical .NET developer. To most .NET developers, PL/SQL will be harder to write and maintain, so you will have to weigh this drawback against the potential gain in performance.

In the following example, we use PL/SQL Associative Arrays to insert 1,000 records, and returning a Ref Cursor at the end. As you can see, there's quite a bit of more code to write:

Ref Cursors

ODP.NET Ref Cursors are objects that point to Oracle server-side cursors (or result sets). The important thing to .NET developers is that a Ref Cursor can be converted to the familiar OracleDataReader. With Ref Cursors, the logic to open result sets can be written entirely in PL/SQL and the results returned to .NET via Ref Cursors.

Why would you want to use Ref Cursors, instead of just doing an ExecuteReader with a SELECT statement? Here are some possible reasons:

  • You need to perform additional work in PL/SQL before returning the result set(s).
  • The user does not have direct access to the table(s) in question.

Here is a real world example. Say you need to update a record in the Orders table and insert a new record into the OrdersAudit table at the same time. Instead of executing two database calls, you can wrap everything into an anonymous PL/SQL block and make one database call.

See the previous section for code example using a Ref Cursor.

Controlling FetchSize

Controlling the FetchSize property is another method to minimize server round-trips. When you read data from the server using the OracleDataReader object, ODP.NET retrieves the data for you in chunks behind the scene. By default, a 64K chunk of data is retrieved each time. However, you can change the chunk size by setting the FetchSize property. By increasing FetchSize, you will lower the number of data retrieval round-trips and increase the overall retrieval operation.

It's typical to set the FetchSize property is to the RowSize (of the OracleCommand object) multiplied by the number of records expected:

When working with a Ref Cursor, the OracleCommand.RowSize property is always zero. You either have to calculate the row size at design time or use reflection at run-time to determine the RowSize by looking at the instance variable m_rowSize on the OracleDataReader object:

On my development PC, when reading 10,000 records from BULK_TEST, setting FetchSize = RowSize * <total number of records> improves the total elapsed time by a factor of two over leaving FetchSize at the default value (985 vs. 407 milliseconds).

Notes

For clarity purposes, the example code in this article does not use the "using" pattern for IDisposable objects. It's recommended that you always use "using" with IDisposable objects such as OracleConnection or OracleCommand.

Summary

In this article, we looked at how various bulk operations in ODP.NET 2.0 can help you improve the performance of your ODP.NET application. Being familiar with these techniques can help you plan, design and implement applications that meet performance goals.

References

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Chinh Do

My blog is at http://www.chinhdo.com

View complete profile here.

Other articles in this category


A Feature-driven Comparison of Entity Framework and NHibernate-2nd Level Caching
Where would you place caching in your layered solution? Two main patterns exist for caching in appli...
A Feature-driven Comparison of Entity Framework and NHibernate - Queries
Let's explore what Entity Framework and NHibernate has to offer when it comes to their query capabil...
A Feature-driven Comparison of Entity Framework and NHibernate—Fetch Plans
This article is about fetch plans-a recognized and common way for developers to instruct the O/RM ab...
A Feature-driven Comparison of Entity Framework and NHibernate - Self-tracking entities
In this article, Dino Esposito introduces self-tracking entities.
.NET type generation for NHibernate mapping collections
An overview of .NET type generation for NHibernate mapping collections.

You might also be interested in the following related blog posts


Migrate from System.Data.OracleClient in two simple steps with Devart read more
System.Data.OracleClient Deprecated read more
Better XAML By Farr: WPF Line of Business Seminar read more
WPF / Silverlight: RadChart and BindableLinq read more
CodeDigest.Com Article,Codes,FAQs - April,2009 read more
LINQ to SQL and alternate Providers read more
.NET RIA Services MIX '09 Talk - Slides + Code read more
Microsoft Ajax 4.0 Preview 4 now available read more
Convert Web.UI Grid into a data entry spreadsheet read more
EF Providers on the horizon read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Amaizing information!!! Garcia Chavez 2/12/2010 12:49 AM
RE: Amaizing information!!! Chinh Do 2/12/2010 9:46 AM
placeholder Bulk Copy with Oracle using Array Binding Eliza Sahoo 4/13/2010 6:55 AM
Inserting Large number of records v a 6/24/2010 8:01 PM
placeholder RE: Inserting Large number of records Chinh Do 6/25/2010 11:05 AM
Good Information Abhijit Shrikhande 10/20/2010 10:00 AM
placeholder Bulk Insert with 64 bit OS Elavarasan B 4/2/2014 2:58 AM
RE: Bulk Insert with 64 bit OS Sonu Kapoor 4/2/2014 1:23 PM
placeholder Not working in 64 BIT OS Elavarasan B 4/2/2014 3:03 AM
Nice Kazi Manzur Rashid 10/31/2007 4:36 AM
placeholder Re: Nice Chinh Do 10/31/2007 12:23 PM
Excelent Article Debugger Us 11/2/2007 2:11 PM
placeholder Re: Excelent Article Chinh Do 11/5/2007 12:13 PM

Please login to rate or to leave a comment.