一、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(); } }