JDBC for MySQL - Herong's Tutorial Examples - v3.12, by Herong Yang
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:
/* MySqlPerformancePreparedStatement.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.util.*; import java.sql.*; import javax.sql.*; public class MySqlPerformancePreparedStatement { 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(); ds.setServerName("localhost"); ds.setPortNumber(3306); ds.setDatabaseName("HerongDB"); ds.setUser("Herong"); ds.setPassword("TopSecret"); 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 (?, ?)"); Random r = new 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) { System.err.println("Exception: "+e.getMessage()); e.printStackTrace(); } } }
Here is the result on a macOS system with a 2.6 GHz processor, running JDK 17, MySQL 8.0.17 and MySQL Connector/J 8.0.27:
PreparedStatement insert 10000 rows with 4373 milliseconds
As a reference, here is the result on a Windows 7 system with a 2.5GHz processor, running JDK 1.8, MySQL 5.5 and MySQL Connector/J 5.1.36:
PreparedStatement insert 10000 rows with 945810 milliseconds
I am very surprised to see that MySQL 5.5 is so slow to insert rows, about 100 milliseconds to insert 1 row!
As a reference, here is the result on a Windows XP system with a 997MHz processor, running JDK 1.6, MySQL 5.0 and MySQL Connector/J 5.0.7:
PreparedStatement insert 10000 rows with 1281 milliseconds
So why is MySQL 5.5 is so slow comparing to MySQL 5.0? The next tutorial will give some explanation.
Table of Contents
JDBC (Java Database Connectivity) Introduction
MySQL JDBC Driver (MySQL Connector/J)
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