Operation method of inserting 10 million pieces of data into MySQL database table in 88 seconds
- 2021-11-10 11:06:27
- OfStack
The database I used is the mysql database version 5.7
Prepare the database table by yourself first
In fact, when I inserted 10 million pieces of data, I encountered some problems. Now I will solve them first. 1 When I inserted 1 million pieces of data, I reported an error. The information of the console is as follows:
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4232009 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.
The above error occurred because the configuration of max_allowed_packet of the database table was not configured large enough, because the default was 4M, and then I adjusted it to 100M and no error was reported
set global max_allowed_packet = 100*1024*1024*
Remember, after setting, you can log in to the database again to see the set value
show VARIABLES like '%max_allowed_packet%'
The code is as follows:
package insert;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import com.mysql.jdbc.PreparedStatement;
public class InsertTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
final String url = "jdbc:mysql://127.0.0.1/teacher" ;
final String name = "com.mysql.jdbc.Driver" ;
final String user = "root" ;
final String password = "123456" ;
Connection conn = null ;
Class.forName(name); // Specify the connection type
conn = DriverManager.getConnection(url, user, password); // Get a connection
if (conn!= null ) {
System.out.println( " Getting Connection Successfully " );
insert(conn);
} else {
System.out.println( " Failed to get connection " );
}
}
public static void insert(Connection conn) {
// Start time
Long begin = new Date().getTime();
// sql Prefix
String prefix = "INSERT INTO t_teacher (id,t_name,t_password,sex,description,pic_url,school_name,regist_date,remark) VALUES " ;
try {
// Save sql Suffix
StringBuffer suffix = new StringBuffer();
// Set transactions to non-automatic commit
conn.setAutoCommit( false );
// Compared to st , pst It will be better
PreparedStatement pst = (PreparedStatement) conn.prepareStatement( "" ); // Prepare the statement for execution
// Outer loop, total number of committed transactions
for ( int i = 1 ; i <= 100 ; i++) {
suffix = new StringBuffer();
// No. 1 j Submission step size
for ( int j = 1 ; j <= 100000 ; j++) {
// Build SQL Suffix
suffix.append( "('" + uutil.UUIDUtil.getUUID()+ "','" +i*j+ "','123456'" + ",' Male '" + ",' Teachers '" + ",'www.bbk.com'" + ",'XX University '" + ",'" + "2016-08-12 14:43:26" + "',' Remarks '" + ")," );
}
// Build integrity SQL
String sql = prefix + suffix.substring( 0 , suffix.length() - 1 );
// Add execution SQL
pst.addBatch(sql);
// Perform an operation
pst.executeBatch();
// Commit transaction
conn.commit();
// Empty up 1 Added data for the second time
suffix = new StringBuffer();
}
// First-class connection
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// End time
Long end = new Date().getTime();
// Time consuming
System.out.println( "1000 Ten thousand pieces of data insertion takes time : " + (end - begin) / 1000 + " s" );
System.out.println( " Insert complete " );
}
}
Summarize