Several methods and time for executing mysql batch insert in java

Source: Internet
Author: User

Method 1:

Java code

Copy codeThe Code is as follows: conn = DriverManager. getConnection (JDBC_URL, JDBC_USER, JDBC_PASS );
Pstmt = conn
. PrepareStatement ("insert into loadtest (id, data) values (?, ?) ");
For (int I = 1; I <= COUNT; I ++ ){
Pstmt. clearParameters ();
Pstmt. setInt (1, I );
Pstmt. setString (2, DATA );
Pstmt.exe cute ();
}

MyISAM: 246.6 seconds, InnoDB: 360.2 seconds

Method 2: Use transactions without automatic commit

Java code

Copy codeThe Code is as follows: conn = DriverManager. getConnection (JDBC_URL, JDBC_USER, JDBC_PASS );
Conn. setAutoCommit (false );
Pstmt = conn
. PrepareStatement ("insert into loadtest (id, data) values (?, ?) ");
For (int I = 1; I <= COUNT; I ++ ){
Pstmt. clearParameters ();
Pstmt. setInt (1, I );
Pstmt. setString (2, DATA );
Pstmt.exe cute ();
If (I % COMMIT_SIZE = 0 ){
Conn. commit ();
}
}
Conn. commit ();

InnoDB: 31.5 seconds

Method 3: executeBatch

Java code

Copy codeThe Code is as follows: conn = DriverManager. getConnection (JDBC_URL
+ "? RewriteBatchedStatements = true ", JDBC_USER, JDBC_PASS );
Conn. setAutoCommit (false );
Pstmt = conn
. PrepareStatement ("insert into loadtest (id, data) values (?, ?) ");
For (int I = 1; I <= COUNT; I + = BATCH_SIZE ){
Pstmt. clearBatch ();
For (int j = 0; j <BATCH_SIZE; j ++ ){
Pstmt. setInt (1, I + j );
Pstmt. setString (2, DATA );
Pstmt. addBatch ();
}
Pstmt.exe cuteBatch ();
If (I + BATCH_SIZE-1) % COMMIT_SIZE = 0 ){
Conn. commit ();
}
}
Conn. commit ();

InnoDB: 5.2 seconds

Required
1) rewriteBatchedStatements = true
2) useserverprep;ts = true

Method 4: LOAD and then COMMIT

Java code

Copy codeThe Code is as follows: conn = DriverManager. getConnection (JDBC_URL, JDBC_USER, JDBC_PASS );
Conn. setAutoCommit (false );
Pstmt = conn. prepareStatement ("load data local infile ''"
+ "Into table loadtest fields terminated ','");
StringBuilder sb = new StringBuilder ();
For (int I = 1; I <= COUNT; I ++ ){
Sb. append (I + "," + DATA + "\ n ");
If (I % COMMIT_SIZE = 0 ){
InputStream is = new ByteArrayInputStream (sb. toString ()
. GetBytes ());
(Com. mysql. jdbc. Statement) pstmt)
. SetLocalInfileInputStream (is );
Pstmt.exe cute ();
Conn. commit ();
Sb. setLength (0 );
}
}
InputStream is = new ByteArrayInputStream (sb. toString (). getBytes ());
(Com. mysql. jdbc. Statement) pstmt). setLocalInfileInputStream (is );
Pstmt.exe cute ();
Conn. commit ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.