想往某個表中插入幾百萬條數據做下測試,原先的想法,直接寫個循環10W次隨便插入點數據試試吧,好吧,我真的很天真....
1
2
3
4
5
6
7
8
9
10
11
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
private String url = "jdbc:mysql://localhost:3306/test01" ; private String user = "root" ; private String password = "123456" ; @Test public 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); 27 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); } } } } |
輸出結果:OK,用時:738199,單位毫秒,也就是說這種方式與直接數據庫中循環是差不多的。
在討論批量處理之前,先說說遇到的坑,首先,JDBC連接的url中要加rewriteBatchedStatements參數設為true是批量操作的前提,其次就是檢查mysql驅動包時候是5.1.13以上版本(低于該版本不支持),因網上隨便下載了5.1.7版本的,然后執行批量操作(100W條插入),結果因為驅動器版本太低緣故并不支持,導致停止掉java程序后,mysql還在不斷的往數據庫中插入數據,最后不得不停止掉數據庫服務才停下來...
那么低版本的驅動包是否對100W+數據插入就無力了呢?實際還有另外一種方式,效率相比來說還是可以接受的。
使用事務提交方式
先將命令的提交方式設為false,即手動提交conn.setAutoCommit(false);最后在所有命令執行完之后再提交事務conn.commit();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
private String url = "jdbc:mysql://localhost:3306/test01" ; private String user = "root" ; private String password = "123456" ; @Test public 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分鐘這樣,勉強還可以接受。
批量處理
接下來就是批量處理了,注意,一定要5.1.13以上版本的驅動包。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true" ; private String user = "root" ; private String password = "123456" ; @Test public 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秒鐘.
批量操作+事務
然后我就想,要是批量操作+事務提交呢?會不會有神器的效果?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
private String url = "jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true" ; private String user = "root" ; private String password = "123456" ; @Test public 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); } } } } |
以下是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"
好了,先到這里...
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/fnz0/p/5713102.html