【實驗硬件環境】
T440p
【數據庫環境】
Oracle10g,win版
【目標表】
create table emp3(
id number(12),
name nvarchar2(20),
age number(3),
primary key(id)
)
【百萬程序】
package com.hy.lab; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Map; public class BatchInserter { //-- 以下為連接Oracle數據庫的四大參數 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; private static final String USER = "luna"; private static final String PSWD = "1234"; public void insert(int count){ Connection conn = null; PreparedStatement pstmt = null; try{ String sql="insert into emp3(id,name,age) values(?,?,?)"; Class.forName(DRIVER); conn = DriverManager.getConnection(URL, USER, PSWD); conn.setAutoCommit(false); pstmt = conn.prepareStatement(sql); for(int i=0;i<count;i++){ pstmt.setLong(1,i); pstmt.setString(2,i+""); pstmt.setInt(3,i % 100); pstmt.addBatch(); } pstmt.executeBatch(); conn.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } private static String ms2DHMS(long startMs, long endMs) { String retval = null; long secondCount = (endMs - startMs) / 1000; String ms = (endMs - startMs) % 1000 + "ms"; long days = secondCount / (60 * 60 * 24); long hours = (secondCount % (60 * 60 * 24)) / (60 * 60); long minutes = (secondCount % (60 * 60)) / 60; long seconds = secondCount % 60; if (days > 0) { retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s"; } else if (hours > 0) { retval = hours + "h" + minutes + "m" + seconds + "s"; } else if (minutes > 0) { retval = minutes + "m" + seconds + "s"; } else if(seconds > 0) { retval = seconds + "s"; }else { return ms; } return retval + ms; } public static void main(String[] args){ long startMs=System.currentTimeMillis(); BatchInserter bit=new BatchInserter(); bit.insert(1000000); long endMs=System.currentTimeMillis(); System.out.println("Time elapsed:"+ms2DHMS(startMs,endMs)); } }
【千萬程序】
注意,如果直接把上面的參數擴大到千萬,會有oom異常,因此我改寫了參數,將百萬插了十次。
package com.hy.lab; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class BatchInserter2 { //-- 以下為連接Oracle數據庫的四大參數 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; private static final String USER = "luna"; private static final String PSWD = "1234"; public void insert(int from,int to){ Connection conn = null; PreparedStatement pstmt = null; try{ String sql="insert into emp3(id,name,age) values(?,?,?)"; Class.forName(DRIVER); conn = DriverManager.getConnection(URL, USER, PSWD); conn.setAutoCommit(false); pstmt = conn.prepareStatement(sql); for(int i=from;i<to;i++){ pstmt.setLong(1,i); pstmt.setString(2,i+""); pstmt.setInt(3,i % 100); pstmt.addBatch(); } pstmt.executeBatch(); conn.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } private static String ms2DHMS(long startMs, long endMs) { String retval = null; long secondCount = (endMs - startMs) / 1000; String ms = (endMs - startMs) % 1000 + "ms"; long days = secondCount / (60 * 60 * 24); long hours = (secondCount % (60 * 60 * 24)) / (60 * 60); long minutes = (secondCount % (60 * 60)) / 60; long seconds = secondCount % 60; if (days > 0) { retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s"; } else if (hours > 0) { retval = hours + "h" + minutes + "m" + seconds + "s"; } else if (minutes > 0) { retval = minutes + "m" + seconds + "s"; } else if(seconds > 0) { retval = seconds + "s"; }else { return ms; } return retval + ms; } public static void main(String[] args){ long startMs=System.currentTimeMillis(); BatchInserter2 bit=new BatchInserter2(); for(int i=0;i<10;i++){ int start=i*1000000; int end=(i+1)*1000000; bit.insert(start,end); } long endMs=System.currentTimeMillis(); System.out.println("Time elapsed:"+ms2DHMS(startMs,endMs)); } }
【后記】
這種JDBC原生PreparedStatement批量操作大批數據的方式,比oracle自己的批量插入語句和dbms這種方式快出兩個數量級,又具有普適性,很快且便利,建議采用!
參考資料:
https://blog.csdn.net/weixin_30898555/article/details/112126797
END