想往某個表中插入幾百萬條資料做下測試,
原先的想法,直接寫個循環10W次随便插入點資料試試吧,好吧,我真的很天真....
DROP PROCEDURE IF EXISTS proc_initData;--如果存在此存儲過程則删掉
DELIMITER $
CREATE PROCEDURE proc_initData()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i<=100000 DO
INSERT INTO text VALUES(i,CONCAT('姓名',i),'XXXXXXXXX');
SET i = i+1;
END WHILE;
END $
CALL proc_initData();
執行CALL proc_initData()後,本來想想,再慢10W條資料頂多30分鐘能搞定吧,結果我打了2把LOL後,回頭一看,還在執行,此時心裡是徹底懵逼的....待我打完第三把結束後,終于執行完了,這種方法若是讓我等上幾百萬條資料,是不是早上去上班,下午下班回來還沒結束呢?10W條資料,有圖有真相
JDBC往資料庫中普通插入方式
後面查了一下,使用JDBC批量操作往資料庫插入100W+的資料貌似也挺快的,
先來說說JDBC往資料庫中普通插入方式,簡單的代碼大緻如下,循環了1000條,中間加點随機的數值,畢竟自己要拿資料測試,資料全都一樣也不好區分
1 private String url = "jdbc:mysql://localhost:3306/test01";
2 private String user = "root";
3 private String password = "123456";
4 @Test
5 public void Test(){
6 Connection conn = null;
7 PreparedStatement pstm =null;
8 ResultSet rt = null;
9 try {
10 Class.forName("com.mysql.jdbc.Driver");
11 conn = DriverManager.getConnection(url, user, password);
12 String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";
13 pstm = conn.prepareStatement(sql);
14 Long startTime = System.currentTimeMillis();
15 Random rand = new Random();
16 int a,b,c,d;
17 for (int i = 1; i <= 1000; i++) {
18 pstm.setInt(1, i);
19 pstm.setInt(2, i);
20 a = rand.nextInt(10);
21 b = rand.nextInt(10);
22 c = rand.nextInt(10);
23 d = rand.nextInt(10);
24 pstm.setString(3, "188"+a+"88"+b+c+"66"+d);
25 pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);27 pstm.executeUpdate();
28 }
29 Long endTime = System.currentTimeMillis();
30 System.out.println("OK,用時:" + (endTime - startTime));
31 } catch (Exception e) {
32 e.printStackTrace();
33 throw new RuntimeException(e);
34 }finally{
35 if(pstm!=null){
36 try {
37 pstm.close();
38 } catch (SQLException e) {
39 e.printStackTrace();
40 throw new RuntimeException(e);
41 }
42 }
43 if(conn!=null){
44 try {
45 conn.close();
46 } catch (SQLException e) {
47 e.printStackTrace();
48 throw new RuntimeException(e);
49 }
50 }
51 }
52 }
輸出結果:OK,用時:738199,機關毫秒,也就是說這種方式與直接資料庫中循環是差不多的。
在讨論批量處理之前,先說說遇到的坑,首先,JDBC連接配接的url中要加rewriteBatchedStatements參數設為true是批量操作的前提,其次就是檢查mysql驅動包時候是5.1.13以上版本(低于該版本不支援),因網上随便下載下傳了5.1.7版本的,然後執行批量操作(100W條插入),結果因為驅動器版本太低緣故并不支援,導緻停止掉java程式後,mysql還在不斷的往資料庫中插入資料,最後不得不停止掉資料庫服務才停下來...
那麼低版本的驅動包是否對100W+資料插入就無力了呢?實際還有另外一種方式,效率相比來說還是可以接受的。
使用事務送出方式
先将指令的送出方式設為false,即手動送出conn.setAutoCommit(false);最後在所有指令執行完之後再送出事務conn.commit();
1 private String url = "jdbc:mysql://localhost:3306/test01";
2 private String user = "root";
3 private String password = "123456";
4 @Test
5 public void Test(){
6 Connection conn = null;
7 PreparedStatement pstm =null;
8 ResultSet rt = null;
9 try {
10 Class.forName("com.mysql.jdbc.Driver");
11 conn = DriverManager.getConnection(url, user, password);
12 String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";
13 pstm = conn.prepareStatement(sql);
14 conn.setAutoCommit(false);
15 Long startTime = System.currentTimeMillis();
16 Random rand = new Random();
17 int a,b,c,d;
18 for (int i = 1; i <= 100000; i++) {
19 pstm.setInt(1, i);
20 pstm.setInt(2, i);
21 a = rand.nextInt(10);
22 b = rand.nextInt(10);
23 c = rand.nextInt(10);
24 d = rand.nextInt(10);
25 pstm.setString(3, "188"+a+"88"+b+c+"66"+d);
26 pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);
27 pstm.executeUpdate();
28 }
29 conn.commit();
30 Long endTime = System.currentTimeMillis();
31 System.out.println("OK,用時:" + (endTime - startTime));
32 } catch (Exception e) {
33 e.printStackTrace();
34 throw new RuntimeException(e);
35 }finally{
36 if(pstm!=null){
37 try {
38 pstm.close();
39 } catch (SQLException e) {
40 e.printStackTrace();
41 throw new RuntimeException(e);
42 }
43 }
44 if(conn!=null){
45 try {
46 conn.close();
47 } catch (SQLException e) {
48 e.printStackTrace();
49 throw new RuntimeException(e);
50 }
51 }
52 }
53 }
以上代碼插入10W條資料,輸出結果:OK,用時:18086,也就十八秒左右的時間,理論上100W也就是3分鐘這樣,勉強還可以接受。
批量處理
接下來就是批量處理了,注意,一定要5.1.13以上版本的驅動包。
1 private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true";
2 private String user = "root";
3 private String password = "123456";
4 @Test
5 public void Test(){
6 Connection conn = null;
7 PreparedStatement pstm =null;
8 ResultSet rt = null;
9 try {
10 Class.forName("com.mysql.jdbc.Driver");
11 conn = DriverManager.getConnection(url, user, password);
12 String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";
13 pstm = conn.prepareStatement(sql);
14 Long startTime = System.currentTimeMillis();
15 Random rand = new Random();
16 int a,b,c,d;
17 for (int i = 1; i <= 100000; i++) {
18 pstm.setInt(1, i);
19 pstm.setInt(2, i);
20 a = rand.nextInt(10);
21 b = rand.nextInt(10);
22 c = rand.nextInt(10);
23 d = rand.nextInt(10);
24 pstm.setString(3, "188"+a+"88"+b+c+"66"+d);
25 pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);
26 pstm.addBatch();
27 }
28 pstm.executeBatch();
29 Long endTime = System.currentTimeMillis();
30 System.out.println("OK,用時:" + (endTime - startTime));
31 } catch (Exception e) {
32 e.printStackTrace();
33 throw new RuntimeException(e);
34 }finally{
35 if(pstm!=null){
36 try {
37 pstm.close();
38 } catch (SQLException e) {
39 e.printStackTrace();
40 throw new RuntimeException(e);
41 }
42 }
43 if(conn!=null){
44 try {
45 conn.close();
46 } catch (SQLException e) {
47 e.printStackTrace();
48 throw new RuntimeException(e);
49 }
50 }
51 }
52 }
10W輸出結果:OK,用時:3386,才3秒鐘.
批量操作+事務
然後我就想,要是批量操作+事務送出呢?會不會有神器的效果?
1 private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true";
2 private String user = "root";
3 private String password = "123456";
4 @Test
5 public void Test(){
6 Connection conn = null;
7 PreparedStatement pstm =null;
8 ResultSet rt = null;
9 try {
10 Class.forName("com.mysql.jdbc.Driver");
11 conn = DriverManager.getConnection(url, user, password);
12 String sql = "INSERT INTO userinfo(uid,uname,uphone,uaddress) VALUES(?,CONCAT('姓名',?),?,?)";
13 pstm = conn.prepareStatement(sql);
14 conn.setAutoCommit(false);
15 Long startTime = System.currentTimeMillis();
16 Random rand = new Random();
17 int a,b,c,d;
18 for (int i = 1; i <= 100000; i++) {
19 pstm.setInt(1, i);
20 pstm.setInt(2, i);
21 a = rand.nextInt(10);
22 b = rand.nextInt(10);
23 c = rand.nextInt(10);
24 d = rand.nextInt(10);
25 pstm.setString(3, "188"+a+"88"+b+c+"66"+d);
26 pstm.setString(4, "xxxxxxxxxx_"+"188"+a+"88"+b+c+"66"+d);
27 pstm.addBatch();
28 }
29 pstm.executeBatch();
30 conn.commit();
31 Long endTime = System.currentTimeMillis();
32 System.out.println("OK,用時:" + (endTime - startTime));
33 } catch (Exception e) {
34 e.printStackTrace();
35 throw new RuntimeException(e);
36 }finally{
37 if(pstm!=null){
38 try {
39 pstm.close();
40 } catch (SQLException e) {
41 e.printStackTrace();
42 throw new RuntimeException(e);
43 }
44 }
45 if(conn!=null){
46 try {
47 conn.close();
48 } catch (SQLException e) {
49 e.printStackTrace();
50 throw new RuntimeException(e);
51 }
52 }
53 }
54 }
以下是100W資料輸出對比:(5.1.17版本MySql驅動包下測試,交替兩種方式下的資料測試結果對比)
批量操作(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 |
可見有一定的效率提升,但是并不是太明顯,當然因為資料差不算太大,也有可能存在偶然因數,畢竟每項隻測3次。
預編譯+批量操作
網上還有人說使用預編譯+批量操作的方式能夠提高效率更明顯,但是本人親測,效率不高反降,可能跟測試的資料有關吧。
預編譯的寫法,隻需在JDBC的連接配接url中将寫入useServerPrepStmts=true即可,
如:
private String url = "jdbc:mysql://localhost:3306/test01?useServerPrepStmts=true&rewriteBatchedStatements=true"
好了,先到這裡...