最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java中JDBC實(shí)現(xiàn)往MySQL插入百萬級數(shù)據(jù)的實(shí)例代碼

 更新時(shí)間:2017年01月19日 14:40:34   作者:酒香逢  
這篇文章主要介紹了java中JDBC實(shí)現(xiàn)往MySQL插入百萬級數(shù)據(jù)的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

想往某個(gè)表中插入幾百萬條數(shù)據(jù)做下測試,原先的想法,直接寫個(gè)循環(huán)10W次隨便插入點(diǎn)數(shù)據(jù)試試吧,好吧,我真的很天真....

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();

執(zhí)行CALL proc_initData()后,本來想想,再慢10W條數(shù)據(jù)頂多30分鐘能搞定吧,結(jié)果我打了2把LOL后,回頭一看,還在執(zhí)行,此時(shí)心里是徹底懵逼的....待我打完第三把結(jié)束后,終于執(zhí)行完了,這種方法若是讓我等上幾百萬條數(shù)據(jù),是不是早上去上班,下午下班回來還沒結(jié)束呢?10W條數(shù)據(jù),有圖有真相

JDBC往數(shù)據(jù)庫中普通插入方式

后面查了一下,使用JDBC批量操作往數(shù)據(jù)庫插入100W+的數(shù)據(jù)貌似也挺快的,

先來說說JDBC往數(shù)據(jù)庫中普通插入方式,簡單的代碼大致如下,循環(huán)了1000條,中間加點(diǎn)隨機(jī)的數(shù)值,畢竟自己要拿數(shù)據(jù)測試,數(shù)據(jù)全都一樣也不好區(qū)分

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,用時(shí):" + (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);
        }
      }
    }
  }

輸出結(jié)果:OK,用時(shí):738199,單位毫秒,也就是說這種方式與直接數(shù)據(jù)庫中循環(huán)是差不多的。

在討論批量處理之前,先說說遇到的坑,首先,JDBC連接的url中要加rewriteBatchedStatements參數(shù)設(shè)為true是批量操作的前提,其次就是檢查mysql驅(qū)動包時(shí)候是5.1.13以上版本(低于該版本不支持),因網(wǎng)上隨便下載了5.1.7版本的,然后執(zhí)行批量操作(100W條插入),結(jié)果因?yàn)轵?qū)動器版本太低緣故并不支持,導(dǎo)致停止掉java程序后,mysql還在不斷的往數(shù)據(jù)庫中插入數(shù)據(jù),最后不得不停止掉數(shù)據(jù)庫服務(wù)才停下來...

那么低版本的驅(qū)動包是否對100W+數(shù)據(jù)插入就無力了呢?實(shí)際還有另外一種方式,效率相比來說還是可以接受的。

使用事務(wù)提交方式

先將命令的提交方式設(shè)為false,即手動提交conn.setAutoCommit(false);最后在所有命令執(zhí)行完之后再提交事務(wù)conn.commit();

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,用時(shí):" + (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條數(shù)據(jù),輸出結(jié)果:OK,用時(shí):18086,也就十八秒左右的時(shí)間,理論上100W也就是3分鐘這樣,勉強(qiáng)還可以接受。

批量處理

接下來就是批量處理了,注意,一定要5.1.13以上版本的驅(qū)動包。

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,用時(shí):" + (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輸出結(jié)果:OK,用時(shí):3386,才3秒鐘.

批量操作+事務(wù)

然后我就想,要是批量操作+事務(wù)提交呢?會不會有神器的效果?

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,用時(shí):" + (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數(shù)據(jù)輸出對比:(5.1.17版本MySql驅(qū)動包下測試,交替兩種方式下的數(shù)據(jù)測試結(jié)果對比)

批量操作(10W) 批量操作+事務(wù)提交(10W) 批量操作(100W) 批量錯(cuò)作+事務(wù)提交(100W)

OK,用時(shí):3901

OK,用時(shí):3343

OK,用時(shí):44242

OK,用時(shí):39798

OK,用時(shí):4142

OK,用時(shí):2949

OK,用時(shí):44248

OK,用時(shí):39959

OK,用時(shí):3664

OK,用時(shí):2689

OK,用時(shí):44389

OK,用時(shí):39367

可見有一定的效率提升,但是并不是太明顯,當(dāng)然因?yàn)閿?shù)據(jù)差不算太大,也有可能存在偶然因數(shù),畢竟每項(xiàng)只測3次。

預(yù)編譯+批量操作

網(wǎng)上還有人說使用預(yù)編譯+批量操作的方式能夠提高效率更明顯,但是本人親測,效率不高反降,可能跟測試的數(shù)據(jù)有關(guān)吧。

預(yù)編譯的寫法,只需在JDBC的連接url中將寫入useServerPrepStmts=true即可,

如:

復(fù)制代碼 代碼如下:

 private String url = "jdbc:mysql://localhost:3306/test01?useServerPrepStmts=true&rewriteBatchedStatements=true"
 

 好了,先到這里...

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ssm實(shí)現(xiàn)視頻的上傳與播放的示例代碼

    ssm實(shí)現(xiàn)視頻的上傳與播放的示例代碼

    這篇文章主要介紹了ssm實(shí)現(xiàn)視頻的上傳與播放的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Spring中的事件發(fā)布機(jī)制原理解析

    Spring中的事件發(fā)布機(jī)制原理解析

    這篇文章主要介紹了Spring中的事件發(fā)布機(jī)制原理解析,當(dāng)我們關(guān)心spring容器什么時(shí)候刷新,或者想在spring容器刷新的時(shí)候做一些事情,監(jiān)聽關(guān)心的事件,主要就是在ApplicationListener中寫對應(yīng)的事件,需要的朋友可以參考下
    2023-11-11
  • Java Map 按照Value排序的實(shí)現(xiàn)方法

    Java Map 按照Value排序的實(shí)現(xiàn)方法

    Map是鍵值對的集合接口,它的實(shí)現(xiàn)類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。這篇文章主要介紹了Java Map 按照Value排序的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2016-08-08
  • Java語言之LinkedList和鏈表的實(shí)現(xiàn)方法

    Java語言之LinkedList和鏈表的實(shí)現(xiàn)方法

    LinkedList是由傳統(tǒng)的鏈表數(shù)據(jù)結(jié)構(gòu)演變而來的,鏈表是一種基本的數(shù)據(jù)結(jié)構(gòu),它可以動態(tài)地增加或刪除元素,下面這篇文章主要給大家介紹了關(guān)于Java語言之LinkedList和鏈表的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2023-05-05
  • springboot yml定義屬性,下文中${} 引用說明

    springboot yml定義屬性,下文中${} 引用說明

    這篇文章主要介紹了springboot yml定義屬性,下文中${} 引用說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Java子類實(shí)例化總是默認(rèn)調(diào)用父類的無參構(gòu)造操作

    Java子類實(shí)例化總是默認(rèn)調(diào)用父類的無參構(gòu)造操作

    這篇文章主要介紹了Java子類實(shí)例化總是默認(rèn)調(diào)用父類的無參構(gòu)造操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java 中 Form表單數(shù)據(jù)的兩種提交方式

    Java 中 Form表單數(shù)據(jù)的兩種提交方式

    本文給大家分享java中form表單數(shù)據(jù)的兩種提交方式,分別是get從制定的服務(wù)器中獲取數(shù)據(jù),pos方式提交數(shù)據(jù)給指定的服務(wù)器處理,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2016-12-12
  • java中synchronized鎖的升級過程

    java中synchronized鎖的升級過程

    這篇文章主要介紹了java中synchronized鎖的升級過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解SpringBoot中如何使用Reactor模型

    詳解SpringBoot中如何使用Reactor模型

    Reactor模型主要提供了一種在Java虛擬機(jī)上構(gòu)建非阻塞應(yīng)用的方式,這種方式使用了響應(yīng)式編程原理,通過響應(yīng)式流標(biāo)準(zhǔn)來實(shí)現(xiàn),下面我們就來看看它在SpringBoot中是如何使用的吧
    2024-04-04
  • Java流程控制break和continue

    Java流程控制break和continue

    這篇文章主要介紹了Java流程控制break和continue,下面文章圍繞break和continue的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-12-12

最新評論

莲花县| 邓州市| 文水县| 霍邱县| 钟山县| 呼玛县| 昌平区| 高台县| 洪湖市| 台中市| 获嘉县| 莱西市| 织金县| 阿城市| 肥乡县| 丰宁| 青龙| 邵阳县| 安达市| 宿迁市| 定结县| 株洲市| 伊金霍洛旗| 桐乡市| 奉化市| 东兰县| 革吉县| 苏尼特右旗| 孟津县| 博白县| 岳普湖县| 卢氏县| 乐业县| 伊春市| 香河县| 潜江市| 原阳县| 铁岭县| 西峡县| 建始县| 和硕县|