批量資料插入高效 轉寄

來源:互聯網
上載者:User

標籤:begin   system   cut   .com   auto   .exe   ati   執行   dstat   

  1. public static void insert() {  
  2.         // 開時時間  
  3.         Long begin = new Date().getTime();  
  4.         // sql首碼  
  5.         String prefix = "INSERT INTO tb_big_data (count, create_time, random) VALUES ";  
  6.         try {  
  7.             // 儲存sql尾碼  
  8.             StringBuffer suffix = new StringBuffer();  
  9.             // 設定事務為非自動認可  
  10.             conn.setAutoCommit(false);  
  11.             // Statement st = conn.createStatement();  
  12.             // 比起st,pst會更好些  
  13.             PreparedStatement pst = conn.prepareStatement("");  
  14.             // 外層迴圈,總提交事務次數  
  15.             for (int i = 1; i <= 100; i++) {  
  16.                 // 第次提交步長  
  17.                 for (int j = 1; j <= 10000; j++) {  
  18.                     // 構建sql尾碼  
  19.                     suffix.append("(" + j * i + ", SYSDATE(), " + i * j  
  20.                             * Math.random() + "),");  
  21.                 }  
  22.                 // 構建完整sql  
  23.                 String sql = prefix + suffix.substring(0, suffix.length() - 1);  
  24.                 // 添加執行sql  
  25.                 pst.addBatch(sql);  
  26.                 // 執行操作  
  27.                 pst.executeBatch();  
  28.                 // 提交事務  
  29.                 conn.commit();  
  30.                 // 清空上一次添加的資料  
  31.                 suffix = new StringBuffer();  
  32.             }  
  33.             // 頭等串連  
  34.             pst.close();  
  35.             conn.close();  
  36.         } catch (SQLException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.         // 結束時間  
  40.         Long end = new Date().getTime();  
  41.         // 耗時  
  42.         System.out.println("cast : " + (end - begin) / 1000 + " ms");  
  43.     } 

批量資料插入高效 轉寄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.