江南又添往事,桥上过往新人。 1.普通方式插入数据 private String url = "jdbc:mysql://localhost:3306/test01";private String user = "root";private String password = "123456";@Testpublic void Test(){Connection conn = null;PreparedStatement pstm =null;ResultSet rt = null;try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url, user, password); String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";pstm = conn.prepareStatement(sql);Long startTime = System.currentTimeMillis();Random rand = new Random();int a,b,c,d;for (int i = 1; i <= 1000; i++) {pstm.setInt(1, i);pstm.setInt(2, i);a = rand.nextInt(10);b = rand.nextInt(10);c = rand.nextInt(10);d = rand.nextInt(10);pstm.setString(3, "188"+a+"88"+b+c+"66"+d);pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);pstm.executeUpdate(); }Long endTime = System.currentTimeMillis();System.out.println("OK,用时:" + (endTime - startTime)); } catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}finally{if(pstm!=null){try {pstm.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}}} 2.事务的方式提交 private String url = "jdbc:mysql://localhost:3306/test01";private String user = "root";private String password = "123456";@Testpublic void Test(){Connection conn = null;PreparedStatement pstm =null;ResultSet rt = null;try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url, user, password); String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";pstm = conn.prepareStatement(sql);conn.setAutoCommit(false);Long startTime = System.currentTimeMillis();Random rand = new Random();int a,b,c,d;for (int i = 1; i <= 100000; i++) {pstm.setInt(1, i);pstm.setInt(2, i);a = rand.nextInt(10);b = rand.nextInt(10);c = rand.nextInt(10);d = rand.nextInt(10);pstm.setString(3, "188"+a+"88"+b+c+"66"+d);pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);pstm.executeUpdate();}conn.commit();Long endTime = System.currentTimeMillis();System.out.println("OK,用时:" + (endTime - startTime)); } catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}finally{if(pstm!=null){try {pstm.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}}} 以上代码插入10W条数据,输出结果:OK,用时:18086,也就十八秒左右的时间,理论上100W也就是3分钟这样,勉强还可以接受。 3.批量处理 private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true";private String user = "root";private String password = "123456";@Testpublic void Test(){Connection conn = null;PreparedStatement pstm =null;ResultSet rt = null;try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url, user, password); String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";pstm = conn.prepareStatement(sql);Long startTime = System.currentTimeMillis();Random rand = new Random();int a,b,c,d;for (int i = 1; i <= 100000; i++) {pstm.setInt(1, i);pstm.setInt(2, i);a = rand.nextInt(10);b = rand.nextInt(10);c = rand.nextInt(10);d = rand.nextInt(10);pstm.setString(3, "188"+a+"88"+b+c+"66"+d);pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);pstm.addBatch();}pstm.executeBatch();Long endTime = System.currentTimeMillis();System.out.println("OK,用时:" + (endTime - startTime)); } catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}finally{if(pstm!=null){try {pstm.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}}} 10W输出结果:OK,用时:3386,才3秒钟. 4.批量操作+事务 private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true";private String user = "root";private String password = "123456";@Testpublic void Test(){Connection conn = null;PreparedStatement pstm =null;ResultSet rt = null;try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url, user, password); String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";pstm = conn.prepareStatement(sql);conn.setAutoCommit(false);Long startTime = System.currentTimeMillis();Random rand = new Random();int a,b,c,d;for (int i = 1; i <= 100000; i++) {pstm.setInt(1, i);pstm.setInt(2, i);a = rand.nextInt(10);b = rand.nextInt(10);c = rand.nextInt(10);d = rand.nextInt(10);pstm.setString(3, "188"+a+"88"+b+c+"66"+d);pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);pstm.addBatch();}pstm.executeBatch();conn.commit();Long endTime = System.currentTimeMillis();System.out.println("OK,用时:" + (endTime - startTime)); } catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}finally{if(pstm!=null){try {pstm.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}if(conn!=null){try {conn.close();} catch (SQLException e) {e.printStackTrace();throw new RuntimeException(e);}}}} 批量操作(10W)批量操作+事务提交(10W)批量操作(100W)批量错作+事务提交(100W) OK,用时:3901 OK,用时:3343 OK,用时:44242 OK,用时:39798 OK,用时:4142 OK,用时:2949 OK,用时:44248 OK,用时:39959 OK,用时:3664 OK,用时:2689 OK,用时:44389 OK,用时:39367