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

本文探讨了在Java中使用JDBC批量处理SQL语句的方法,对比了使用Statement和PreparedStatement进行批量操作的效率,并通过实验数据说明了在不同场景下批量处理的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

批量处理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);
        }
    }

输出结果为:mysql Preparedstatement batch time=152905
 


@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) {
        }
    }

输出结果为:no batch:120776

怎么看着用批量处理比不是用批量处理还慢呢。

如果使用Oracle并结合使用batch,那么效率是成倍增长的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值