Java使用JDBC向MySQL數(shù)據(jù)庫批次插入10W條數(shù)據(jù)(測試效率)
使用JDBC連接MySQL數(shù)據(jù)庫進行數(shù)據(jù)插入的時候,特別是大批量數(shù)據(jù)連續(xù)插入(100000),如何提高效率呢?
在JDBC編程接口中Statement 有兩個方法特別值得注意:
通過使用addBatch()
和executeBatch()
這一對方法可以實現(xiàn)批量處理數(shù)據(jù)。
不過值得注意的是,首先需要在數(shù)據(jù)庫鏈接中設(shè)置手動提交,connection.setAutoCommit(false)
,然后在執(zhí)行Statement之后執(zhí)行connection.commit()
。
import java.io.BufferedReader; import java.io.IOException; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Date; import com.mysql.jdbc.Connection; public class MysqlBatchUtil { private String sql="INSERT INTO db_test (param1,param2,param3,param4,param5) VALUES (?,?,?,?,?)"; private String charset="utf-8"; private String connectStr="jdbc:mysql://localhost:3306/test"; private String username="root"; private String password="123456"; private void doStore() throws ClassNotFoundException, SQLException, IOException { Class.forName("com.mysql.jdbc.Driver"); connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";//此處是測試高效批次插入,去掉之后執(zhí)行時普通批次插入 Connection conn = (Connection) DriverManager.getConnection(connectStr, username,password); conn.setAutoCommit(false); // 設(shè)置手動提交 int count = 0; PreparedStatement psts = conn.prepareStatement(sql); String line = null; Date begin=new Date(); for(int i=0;i<=100000;i++){ psts.setString(1, i+"param1"); psts.setString(2, i+"param2"); psts.setString(3, i+"param3"); psts.setString(4, i+"param4"); psts.setString(5, i+"param5"); psts.addBatch(); // 加入批量處理 count++; } psts.executeBatch(); // 執(zhí)行批量處理 conn.commit(); // 提交 Date end=new Date(); System.out.println("數(shù)量="+count); System.out.println("運行時間="+(end.getTime()-begin.getTime())); conn.close(); } public static void main(String[] args) { try { new MysqlBatchUtil().doStore(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
測試結(jié)果:
數(shù)量=100001
運行時間=4725
一共10W,執(zhí)行時間一共花費 47 秒.
這個效率仍然不高,似乎沒有達到想要的效果,需要進一步改進。
在MySQL JDBC連接字符串中還可以加入?yún)?shù),
rewriteBatchedStatements=true
mysql默認關(guān)閉了batch處理,通過此參數(shù)進行打開,這個參數(shù)可以重寫向數(shù)據(jù)庫提交的SQL語句
useServerPrepStmts=false
如果不開啟(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement進行本地SQL拼裝,最后送到db上就是已經(jīng)替換了?后的最終SQL.
在此稍加改進,連接字符串中加入下面語句(代碼構(gòu)造方法中去掉注釋):
connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";
再次測試結(jié)果如下:
數(shù)量=100001
運行時間=1213
同樣的數(shù)據(jù)量,這次執(zhí)行只花費了12秒 ,由此可見處理效率大大提高,呵呵
以上所述是小編給大家介紹的Java使用JDBC向MySQL數(shù)據(jù)庫批次插入10W條數(shù)據(jù)測試效率,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringMVC中的ConversionServiceExposingInterceptor工具類解析
這篇文章主要介紹了SpringMVC中的ConversionServiceExposingInterceptor工具類解析,ConversionServiceExposingInterceptor是Spring MVC的一個HandlerInterceptor,用于向請求添加一個屬性,需要的朋友可以參考下2023-12-12java中如何使用BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR
這篇文章主要介紹了java中如何BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR的相關(guān)資料,需要的朋友可以參考下2017-03-03Spring boot調(diào)用Oracle存儲過程的兩種方式及完整代碼
這篇文章主要給大家介紹了關(guān)于Spring boot調(diào)用Oracle存儲過程的兩種方式及完整代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2020-08-08Spring實戰(zhàn)之抽象Bean和子Bean定義與用法示例
這篇文章主要介紹了Spring實戰(zhàn)之抽象Bean和子Bean定義與用法,結(jié)合實例形式分析了Spring抽象Bean和子Bean相關(guān)配置、定義與使用操作技巧,需要的朋友可以參考下2019-11-11Springboot集成RabbitMQ死信隊列的實現(xiàn)
在大多數(shù)的MQ中間件中,都有死信隊列的概念。本文主要介紹了Springboot集成RabbitMQ死信隊列的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09spring cloud eureka微服務(wù)之間的調(diào)用詳解
這篇文章主要介紹了spring cloud eureka微服務(wù)之間的調(diào)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07