Performance of Inserting Rows with a PreparedStatement

This section describes how to measure the performance of inserting rows using a PreparedStatement object.

Running SQL statements using PreparedStatement objects is supposed to be faster than using regular Statement objects. To test this, I wrote the following Java program to measure the performance of inserting rows using a PreparedStatement object into an empty table:

/* SqlServerPerformancePreparedStatement.java
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
import java.sql.*;
public class SqlServerPerformancePreparedStatement {
  public static void main(String [] args) {
    Connection con = null;
    try {
      com.microsoft.sqlserver.jdbc.SQLServerDataSource ds 
        = new com.microsoft.sqlserver.jdbc.SQLServerDataSource();
      ds.setServerName("localhost");
//      ds.setPortNumber(60782);
      ds.setInstanceName("SQLEXPRESS");
      ds.setDatabaseName("AdventureWorks2014");
      ds.setUser("Herong");
      ds.setPassword("T0pSecret");
      con = ds.getConnection();

// Delete all rows from the table
      Statement sta = con.createStatement();
      sta.executeUpdate("DELETE FROM Profile");

// Start the test
     int count = 10000;
     long t1 = System.currentTimeMillis();

// PreparedStatement to insert rows
      PreparedStatement ps = con.prepareStatement(
	"INSERT INTO Profile (FirstName, LastName) VALUES (?, ?)");
      java.util.Random r = new java.util.Random();
      for (int i = 0; i < count; i++) {
      	ps.setString(1,Integer.toHexString(r.nextInt(9999)));
      	ps.setString(2,Integer.toHexString(r.nextInt(999999)));
        ps.executeUpdate();
      }      
      ps.close();

// End the test
     long t2 = System.currentTimeMillis();
     System.out.println("PreparedStatement insert "+count
         +" rows with "+(t2 -t1) +" milliseconds");

      con.close();        
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Here is the result on a Windows 7 system with a 2.5GHz processor, running JDK 1.8, SQL Server 2014 and JDBC Driver 4.2:

PreparedStatement insert 10000 rows with 9701 milliseconds

I am very surprised to see that SQL Server 2014 and JDK 1.8 is slower than SQL Server 2005 and JDK 1.6! Here is the result on a Windows XP system with a 997MHz processor, running JDK 1.6, SQL Server 2005 and JDBC Driver 1.0:

PreparedStatement insert 10000 rows with 5625 milliseconds

The performance drop might be related to some configurations in the SQL Server that I missed. What do you think?

Last update: 2015.

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc42.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

SQL Server - PreparedStatement

 Create a New User in SQL Server

 Creating a Table with an IDENTITY Column

 Inserting Rows to the Test Table

 PreparedStatement Overview

 PreparedStatement with Parameters

 PreparedStatement in Batch Mode

Performance of Inserting Rows with a PreparedStatement

 Performance of Inserting Rows with a Regular Statement

 Performance of Inserting Rows with a ResultSet

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 Outdated Tutorials

 References

 PDF Printing Version