一、MYSQL版(约半小时插入2k500w条记录,本地内存8G)
MYSQL 批量插入SQL格式
INSERT INTO TABLE_NAME (...) VALUES (...),(...),(...)
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Date; import java.util.Random; import java.util.UUID; public class BatchInsert { static Connection conn = null; public static void initConn() { String url = "jdbc:mysql://localhost:3306/test?" + "user=root&password=5555&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC"; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url); } catch (Exception e) { e.printStackTrace(); } } public static void insert() { // 开时时间 Long begin = new Date().getTime(); // sql前缀 String prefix = "INSERT INTO stu_info (...) VALUES "; // ... 对应全部字段名 try { // 保存sql后缀 StringBuffer suffix = new StringBuffer(); // 设置事务为非自动提交 conn.setAutoCommit(false); // Statement st = conn.createStatement(); // 比起st,pst会更好些 PreparedStatement pst = conn.prepareStatement(""); // 外层循环,总提交事务次数 for (int i = 1; i <= 2500; i++) { // 一次批量插入数据数,这里设置成10000正常运行,10w则报错,应该可以通过配置MYSQL参数增加批量插入的数据量 for (int j = 1; j <= 10000; j++) { // 构建sql后缀 suffix.append("(" ... "),"); // ...对应全部字段值 } // 构建完整sql String sql = prefix + suffix.substring(0, suffix.length() - 1); // 添加执行sql System.out.println(new Date().toString()+",已经插入记录条数:"+(i*10000)); pst.addBatch(sql); // 执行操作 pst.executeBatch(); // 提交事务 conn.commit(); // 清空上一次添加的数据 suffix = new StringBuffer(); } pst.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } // 结束时间 Long end = new Date().getTime(); // 耗时 System.out.println("cast : " + (end - begin) / 1000 + " ms"); } public static void main(String[] args) throws SQLException, ClassNotFoundException { initConn(); insert(); } }
2、ORACLE版(本地测试 ORACLE插入千条记录效率不及MYSQL插入万条记录)
MYSQL 批量插入SQL格式
INSERT ALL INTO TABLE_NAME (...) VALUES (...)
INTO TABLE_NAME(...) VALUES(...)
INTO TABLE_NAME(...) VALUES(...)
...
...
...
import java.sql.*; import java.util.Date; import java.util.Random; import java.util.UUID; /** * @program: pthink-generator->TestNGInsert * @description: * @author: WuDG * @create: 2019-08-14 00:51 **/ @SuppressWarnings("all") public class TestNGInsert { static Connection conn = null; public static void initConn() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = ""; String username = ""; String password = ""; conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } } public static void insert() { // 开时时间 Long begin = new Date().getTime(); // sql前缀 String prefix = "INSERT ALL "; try { // 保存sql后缀 StringBuffer suffix = new StringBuffer(); // 设置事务为非自动提交 conn.setAutoCommit(false); Statement st = conn.createStatement();// 外层循环,总提交事务次数 for (int i = 1; i <= 25000; i++) { // 一次批量插入数据数,这里设置成1000正常运行,1w则报错,应该可以通过配置MYSQL参数增加批量插入的数据量 for (int j = 1; j <= 1000; j++) { // 构建sql后缀 suffix.append("INTO table_name(...) VALUES (" ...") "); } // 构建完整sql String sql = prefix + suffix + " select * from dual";// 添加执行sql st.addBatch(sql); // 执行操作 st.executeBatch(); // 提交事务 conn.commit(); System.out.println(new Date().toString() + ",已经插入记录条数:" + (i * 1000)); // 清空上一次添加的数据 suffix = new StringBuffer(); } st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } // 结束时间 Long end = new Date().getTime(); // 耗时 System.out.println("COST : " + (end - begin) / 1000 + " s"); } public static void main(String[] args) throws SQLException, ClassNotFoundException { initConn(); insert(); } }