MySQL的批處理

來源:互聯網
上載者:User

標籤:程式   參數   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的批處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.