二次元的記憶,瑞口掰吶睿
阿新 • • 發佈:2019-02-12
10萬條資料插入Sql Server
springMVC+ibatis, 用spring的事務機制通過ibatis來插入, 效果不理想, 10萬條資料11秒左右完成
用jdbc插入10萬條,3秒完成,還算過關
//10萬條 5秒 Connection conn = sqlMapClient.getDataSource().getConnection(); conn.setAutoCommit(false); PreparedStatement pstat = conn.prepareStatement("insert into ttt(" + "ttt,ttt,ttt,ttt,ttt" + ") values(" + "?,?,?,?,?)"); try { int num = 0; for(Class d: dList) { num ++; pstat.setString(1, d.getTtt()); pstat.setString(2, d.getTtt()); pstat.setString(3, d.getTtt()); pstat.setLong(4, d.getTtt()); pstat.setInt(5, d.getTtt()); pstat.addBatch(); if(num > 10000){ pstat.executeBatch(); pstat.clearBatch(); conn.commit(); num = 0; } } pstat.executeBatch(); pstat.clearBatch(); conn.commit(); } catch (Exception e) { conn.rollback();
pstat.close();
conn.close();
throw e; } pstat.close(); conn.close();
一萬條資料插入一次比較理想, 均在0.3秒內完成