標籤:begin system cut .com auto .exe ati 執行 dstat
- public static void insert() {
- // 開時時間
- Long begin = new Date().getTime();
- // sql首碼
- String prefix = "INSERT INTO tb_big_data (count, create_time, random) VALUES ";
- try {
- // 儲存sql尾碼
- StringBuffer suffix = new StringBuffer();
- // 設定事務為非自動認可
- conn.setAutoCommit(false);
- // Statement st = conn.createStatement();
- // 比起st,pst會更好些
- PreparedStatement pst = conn.prepareStatement("");
- // 外層迴圈,總提交事務次數
- for (int i = 1; i <= 100; i++) {
- // 第次提交步長
- for (int j = 1; j <= 10000; j++) {
- // 構建sql尾碼
- suffix.append("(" + j * i + ", SYSDATE(), " + i * j
- * Math.random() + "),");
- }
- // 構建完整sql
- String sql = prefix + suffix.substring(0, suffix.length() - 1);
- // 添加執行sql
- pst.addBatch(sql);
- // 執行操作
- pst.executeBatch();
- // 提交事務
- conn.commit();
- // 清空上一次添加的資料
- suffix = new StringBuffer();
- }
- // 頭等串連
- pst.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // 結束時間
- Long end = new Date().getTime();
- // 耗時
- System.out.println("cast : " + (end - begin) / 1000 + " ms");
- }
批量資料插入高效 轉寄