天天看點

批量處理---提高處理速度

批量處理JDBC語句提高處理速度。

    當需要成批插入或者更新記錄時。可以采用Java的批量更新機制,這一機制允許多條語句一次性送出給資料庫批量處理。

    通常情況下比單獨送出處理更有效率lJDBC的批量處理語句包括下面兩個方法:

        addBatch(String):添加需要批量處理的SQL語句或是參數;

        executeBatch():執行批量處理語句;

    通常我們會遇到兩種批量執行SQL語句的情況:

        用Statement實作批量處理;

        用PreparedStatement實作批量處理;

測試:

@Test
    public void testStatementBatch() {
        Connection conn = null;
        Statement stmt = null;
        long time = System.currentTimeMillis();
        String sql = "";
        try {
            conn = DBUtil.getCon();
            stmt = conn.createStatement();
            for (int i = 0; i < 5000; i++) {
                sql = "INSERT INTO b_user(id,NAME,PASSWORD,age) VALUES(1,'vincent','123456',1)";
                stmt.addBatch(sql);
            }
            stmt.executeBatch();
            System.out.println("mysql Statement batch time="
                    + (System.currentTimeMillis() - time));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }      

輸出結果為:mysql Statement batch time=147716

@Test
    public void testPreparedStatementBatch() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        conn = DBUtil.getCon();
        String sql = "INSERT INTO b_user(id,NAME,PASSWORD,age) VALUES(?,?,?,?)";
        long time = System.currentTimeMillis();
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < 5000; i++) {
                pstmt.setInt(1, 1);
                pstmt.setString(2, "t");
                pstmt.setString(3, "t");
                pstmt.setInt(4, 1);
                pstmt.addBatch();
                if(i%500==0){
                //防止記憶體溢出
                    pstmt.execuBatch();
                    pstmt.clearBatch();
                }
            }
            pstmt.executeBatch();
            System.out.println("mysql Preparedstatement batch time="
                    + (System.currentTimeMillis() - time));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
    }      
@Test
    public void testEff() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        long time = System.currentTimeMillis();
        conn = DBUtil.getCon();
        
        String sql = "INSERT INTO b_user(id,NAME,PASSWORD,age) VALUES(?,?,?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < 5000; i++) {
                pstmt.setInt(1, 1);
                pstmt.setString(2, "t");
                pstmt.setString(3, "t");
                pstmt.setInt(4, 1);
                pstmt.executeUpdate();
            }
            System.out.println("no batch:"+(System.currentTimeMillis()-time));
        } catch (Exception e) {
        }
    }