If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > JAVA > Statement VS PreparedStatement

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-15-03, 20:05
kcmyl kcmyl is offline
Registered User
 
Join Date: Aug 2003
Location: San Jose
Posts: 2
Statement VS PreparedStatement

In the application I am working on, there is the need to speed up the insert of a variable (but large) number of rows to a table. The inserts are simple, and right now we are using ordinary Statement objects. We thought we can speed it up by using PreparedStatement or CallableStatement to stored procedure. Our tests show that using PreparedStatement is slower than using Statement, and that using CallableStatement is the slowest of the three? Our application uses transaction service provided by COM+ (MSDTC), JDBC connection through MS ODBC for Oracle (2.573) and Oracle 8i (8.1.7).

Is there any good explanation for this?

Also, to determine if the MS ODBC driver was part of the problem I wrote some test app using the Oracle thin JDBC driver:

XXXXX

Class.forName("oracle.jdbc.driver.OracleDriver");
conn = java.sql.DriverManager.getConnection("jdbcracle: thin:....@......");
Statement del = conn.createStatement();
del.executeUpdate("delete from test_jdbc");

conn.setAutoCommit(false);
EventLog.log("Connection established!");

et.start();
Statement stmt = conn.createStatement();
int i = 0;
for(i = 0; i < 5000; i++){
String sql = "insert into test_jdbc values (" + i + "," + "'Peter', 'Some description goes here'," +
"1.2, Date '" + dd.toString() + "', 78458," +
i + "," + i + "," + i + "," + i + "," + i + ")";
stmt.executeUpdate(sql);
}
conn.commit();
et.stop();
et.logElapseTime(null, "Statement: ");

del.executeUpdate("delete from test_jdbc");
String sql = "insert into test_jdbc values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
et.start();
PreparedStatement pstmt = conn.prepareStatement(sql);
for(i = 0; i < 5000; i++){
pstmt.setInt(1, i);
pstmt.setString(2, "Peter");
pstmt.setString(3, "Some description goes here");
pstmt.setDouble(4, 1.2);
pstmt.setDate(5, dd);
pstmt.setLong(6, 78458);
pstmt.setInt(7, i);
pstmt.setInt(8, i);
pstmt.setInt(9, i);
pstmt.setInt(10, i);
pstmt.setInt(11, i);
pstmt.executeUpdate();
}
conn.commit();
et.stop();
et.logElapseTime(null, "PreparedStatement: ");
conn.close();

XXXX

I found that using Statement is about as fast as using PreparedStatement for 5000 inserts. Is there something wrong with the way I am using them or with the setup?

I appreciate any help.

Thanks.

-kcmyl
Reply With Quote
  #2 (permalink)  
Old 08-21-03, 14:18
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,225
Re: Statement VS PreparedStatement

Quote:
Originally posted by kcmyl
The inserts are simple, and right now we are using ordinary Statement objects. We thought we can speed it up by using PreparedStatement or CallableStatement to stored procedure. Our tests show that using PreparedStatement is slower than using Statement, and that using CallableStatement is the slowest of the three?
I don't think you should expect improved performance of PreparedStatements in your case. Improvement comes from the fact that the SQL optimizer would analyze the PreparedStatement only once and then re-use the execution plan for all subsequent executions of that statement. However, in case of simple insert statements the optimizer overhead is negligible - there's nothing to optimize.

Among the most common ways to increase performance of inserts are:
- reduce the number of indexes on the table
- disable triggers and constraints
- optimize free space management (may be PCTFREE is too high?)
- optimize space allocation (extent size and number, etc).

Hope this helps,

Nick
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On