Performance of Inserting Rows with a Regular Statement

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

To compare the performance result from the previous result with regular Statement objects. I wrote the following Java program to measure the performance of inserting rows using a regular Statement object into an empty table:

 * Copyright (c) All Rights Reserved.
import java.util.*;
import java.sql.*;
import javax.sql.*;
public class MySqlPerformanceRegularStatement {
  public static void main(String [] args) {
    Connection con = null;
    try {
      com.mysql.cj.jdbc.MysqlDataSource ds
        = new com.mysql.cj.jdbc.MysqlDataSource();
      // com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
      //   = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();

      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();

// Regular Statement to insert rows
      Statement rs = con.createStatement();
      Random r = new Random();
      for (int i = 0; i < count; i++) {
        rs.executeUpdate("INSERT INTO Profile (FirstName, LastName)"
          +" VALUES ('"+Integer.toHexString(r.nextInt(9999))
          +"', '"+Integer.toHexString(r.nextInt(999999))+"')");

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

    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());

Here is the result on a macOS system with a 2.6 GHz processor, running JDK 13, MySQL 8.0.17 and MySQL Connector/J 8.0.19:

Regular Statement insert 10000 rows with 3348 milliseconds

Here is summary of performance results on this program in the past with different versions of storage engine, MySQL, Java, and JDBC driver:

Operation: Regular INSERT

Execution   Storage   MySQL    Java      JDBC     Computer    CPU
Time (ms)   Engine    Server   Version   Driver   System      GHz
---------   -------   ------   -------   ------   -------     ---
     3348   MyISAM    8.0      13        8.0      macOS 10    2.6
      915   MyISAM    5.5       8        5.1      Windows 7   2.5
   922212   InnoDB    5.5       8        5.1      Windows 7   2.5
     1297   MyISAM    5.0       6        5.0      Windows XP  1.0

As you can see, MySQL and JDBC driver 8.0 is much slower than MySQL and JDBC driver 5.5. Why? Maybe macOS 10 is slower than Windows 7?

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

MySQL - PreparedStatement

 PreparedStatement Overview

 PreparedStatement with Parameters

 PreparedStatement in Batch Mode

 Performance of Inserting Rows with a PreparedStatement

 InnoDB (MySQL 5.5 Default Engine) Slower on INSERT

Performance of Inserting Rows with a Regular Statement

 Performance of Inserting Rows with a ResultSet

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Using Connection Pool with JDBC

 Archived Tutorials


 Full Version in PDF/EPUB