Teach you how to insert 1 million records into MySQL in 6 seconds

Teach you how to insert 1 million records into MySQL in 6 seconds

1. Idea

It only took 6 seconds to insert 1,000,000 records into MySQL!

Key points:

1. Using PreparedStatement Object


2. rewriteBatchedStatements=true turns on batch inserts, inserts are executed only once, and all inserts are faster.

2. Code

package test0823.demo1;

import java.sql.*;

/**
 * @author : Bei-Zhen
 * @date : 2020-08-24 0:43
 */
public class JDBC2 {

  //static int count = 0;

  public static void main(String[] args) {

    long start = System.currentTimeMillis();
    conn();
    long end = System.currentTimeMillis();
    System.out.println("Time taken: " + (end - start)/1000 + "seconds");
  }

  public static void conn(){
    //1. Import the driver jar package //2. Register the driver (the driver jar package after mysql5 can omit the driver registration step)
    //Class.forName("com.mysql.jdbc.Driver");
    //3. Get the database connection object Connection conn = null;
    PreparedStatement pstmt = null;
    {
      try {
        //"&rewriteBatchedStatements=true", insert multiple data at a time, insert only onceconn = DriverManager.getConnection("jdbc:mysql:///test?" + "&rewriteBatchedStatements=true","root","root");
        //4. Define sql statement String sql = "insert into user values(default,?,?)";
        //5. Get the PreparedStatement object that executes SQL
        pstmt = conn.prepareStatement(sql);
        //6. Continuously generate sql
        for (int i = 0; i < 1000000; i++) {
          pstmt.setString(1,(int)(Math.random()*1000000)+"");
          pstmt.setString(2,(int)(Math.random()*1000000)+"");
          pstmt.addBatch();
        }
        //7. Insert data into the database once pstmt.executeBatch();
        System.out.println("1,000,000 pieces of information were added successfully!");

      } catch (SQLException e) {
        e.printStackTrace();
      finally
        //8. Release resources //Avoid null pointer exception if (pstmt != null) {
          try {
            pstmt.close();
          } catch (SQLException e) {
            e.printStackTrace();
          }
        }

        if(conn != null) {
          try {
            conn.close();
          } catch (SQLException e) {
            e.printStackTrace();
          }
        }
      }
    }

  }

}

3. Operation Results

1,000,000 pieces of information were added successfully!
Time taken: 6 seconds


This is the end of this article on how to insert 1 million records into MySQL in 6 seconds. For more information about how to insert 1 million records into MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySql quick insert tens of millions of large data examples
  • How to insert 10 million records into a MySQL database table in 88 seconds
  • Example code for inserting millions of data into MySQL using JDBC in Java
  • How to quickly insert millions of test data in MySQL

<<:  Linux uses lsof/extundelete tools to restore accidentally deleted files or directories

>>:  Comparison of the advantages of vue3 and vue2

Recommend

JavaScript implements large file upload processing

Many times when we process file uploads, such as ...

MySQL 8.x msi version installation tutorial with pictures and text

1. Download MySQL Official website download addre...

MySQL spatial data storage and functions

Table of contents 1. Data Type 1. What is MySQL s...

Nginx configuration to achieve multiple server load balancing

Nginx load balancing server: IP: 192.168.0.4 (Ngi...

MySQL index usage instructions (single-column index and multi-column index)

1. Single column index Choosing which columns to ...

Summary of some common methods of JavaScript array

Table of contents 1. How to create an array in Ja...

Steps to use ORM to add data in MySQL

【Foreword】 If you want to use ORM to operate data...

Detailed explanation of Tomcat core components and application architecture

Table of contents What is a web container? The Na...

Share 20 excellent web form design cases

Sophie Hardach Clyde Quay Wharf 37 East Soapbox Rx...

How to uninstall MySQL 5.7 on CentOS7

Check what is installed in mysql rpm -qa | grep -...

How are spaces represented in HTML (what do they mean)?

In web development, you often encounter characters...

CSS3 to achieve floating cloud animation

Operation effect html <head> <meta chars...

Detailed explanation of creating, calling and managing MySQL stored procedures

Table of contents Introduction to stored procedur...

Solution to the inaccessibility of Tencent Cloud Server Tomcat port

I recently configured a server using Tencent Clou...