標籤:程式 參數 class mysql參數 批處理 dstat 狀態 代碼 tca
MySQL預設是關閉批處理的,所以我們在預設狀態下(批處理未開啟)向資料庫中存入10000條資料,核心代碼如下:
package cn.itcast.demo5;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import org.junit.Test;import cn.itcast.demo3.JdbcUtils;public class Demo5 { @Test public void fun5() throws SQLException { /* * pstmt: * > 添加參數到批中 * > 執行批! */ Connection con = JdbcUtils.getConnection(); String sql = "INSERT INTO t_stu VALUES(?,?,?,?)"; PreparedStatement pstmt = con.prepareStatement(sql); // 瘋狂的添加參數 for(int i = 0; i < 10000; i++) { pstmt.setInt(1, i+1); pstmt.setString(2, "stu_" + i); pstmt.setInt(3, i); pstmt.setString(4, i%2==0?"男":"女"); pstmt.addBatch();//添加批!這一組參數就儲存到集合中了。 } long start = System.currentTimeMillis(); pstmt.executeBatch();//執行批! long end = System.currentTimeMillis(); System.out.println(end - start); }}
上述程式執行結束耗費時間412764MS
這是開啟MySQL的批處理,開啟檔案:
將MySQL參數 url=jdbc:mysql://localhost:3306/exam
改為 url=jdbc:mysql://localhost:3306/exam?rewriteBatchedStatements=true
再次執行程式,耗時301MS,速度快了1000倍以上!
MySQL的批處理