亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java使用JDBC向MySQL數(shù)據(jù)庫批次插入10W條數(shù)據(jù)(測試效率)

 更新時(shí)間:2016年12月29日 10:42:27   作者:落葉  
使用JDBC連接MySQL數(shù)據(jù)庫進(jìn)行數(shù)據(jù)插入的時(shí)候,特別是大批量數(shù)據(jù)連續(xù)插入(100000),如何提高效率呢?今天小編通過本教程給大家介紹下

使用JDBC連接MySQL數(shù)據(jù)庫進(jìn)行數(shù)據(jù)插入的時(shí)候,特別是大批量數(shù)據(jù)連續(xù)插入(100000),如何提高效率呢?

在JDBC編程接口中Statement 有兩個(gè)方法特別值得注意:

通過使用addBatch()executeBatch()這一對方法可以實(shí)現(xiàn)批量處理數(shù)據(jù)。

不過值得注意的是,首先需要在數(shù)據(jù)庫鏈接中設(shè)置手動(dòng)提交,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í)行時(shí)普通批次插入
  Connection conn = (Connection) DriverManager.getConnection(connectStr, username,password); 
  conn.setAutoCommit(false); // 設(shè)置手動(dòng)提交 
  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("運(yùn)行時(shí)間="+(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
運(yùn)行時(shí)間=4725

一共10W,執(zhí)行時(shí)間一共花費(fèi) 47 秒.

這個(gè)效率仍然不高,似乎沒有達(dá)到想要的效果,需要進(jìn)一步改進(jìn)。

在MySQL JDBC連接字符串中還可以加入?yún)?shù),

rewriteBatchedStatements=true

mysql默認(rèn)關(guān)閉了batch處理,通過此參數(shù)進(jìn)行打開,這個(gè)參數(shù)可以重寫向數(shù)據(jù)庫提交的SQL語句

useServerPrepStmts=false

如果不開啟(useServerPrepStmts=false),使用com.mysql.jdbc.PreparedStatement進(jìn)行本地SQL拼裝,最后送到db上就是已經(jīng)替換了?后的最終SQL.

在此稍加改進(jìn),連接字符串中加入下面語句(代碼構(gòu)造方法中去掉注釋):

connectStr += "?useServerPrepStmts=false&rewriteBatchedStatements=true";

再次測試結(jié)果如下:

數(shù)量=100001
運(yùn)行時(shí)間=1213

同樣的數(shù)據(jù)量,這次執(zhí)行只花費(fèi)了12秒 ,由此可見處理效率大大提高,呵呵

以上所述是小編給大家介紹的Java使用JDBC向MySQL數(shù)據(jù)庫批次插入10W條數(shù)據(jù)測試效率,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論