zoukankan      html  css  js  c++  java
  • JDBC的批处理学习rewriteBatchedStatements=true

    如果在不添加批处理指令的情况下,mysql默认是不使用批处理操作,如果在url尾部添加rewriteBatchedStatements=true

    可以使当前连接 使用批处理操作

    创建数据库表结构

    create table test(
    id int primary key auto_increment,
    name varchar(20),
    age int,
    sex varchar(10));

    未使用批出的url链是

    jdbc:mysql://localhost:3306/mysql1

    测试源码是:

    package JDBCDemo;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    import org.junit.Test;
    
    import JDBCUtils.jdbcUtils;
    
    public class JDBCdemo5 {
        /*
         * 批处理实验 在批处理情况下 速度更快 
         * 在url之后添加rewriteBatchedStatements=true 开启批出操作
         */
        @Test
        public void DoMore() throws Exception{
            /*
             * 依然是四大对象 获取连接
             */
            Connection connection =jdbcUtils.getConnection();
            
            String sql="insert into test values(?,?,?,?)";
            
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            
            for (int i = 1; i < 20002; i++) {
                preparedStatement.setInt(1, i);
                preparedStatement.setString(2, "o153014841"+i);
                preparedStatement.setInt(3, i);
                preparedStatement.setString(4, i%2==0? "":"");
                
                preparedStatement.addBatch();
            }
            long start=System.currentTimeMillis();
            preparedStatement.executeBatch();
            long end=System.currentTimeMillis();
            System.out.println(end-start);
        }
    }

    测试结果为

    由于在测试时原本只想测试2000个数据 但是一不小心测试为2万条数据 造成时间比较长 这是测试的疏忽,

    然后测试使用的jdbcUtils只是获取connection连接 不具备其他功能 然后测试可以使用批出指令的时间为

    前后结果相差巨大 批处理速度快很多

  • 相关阅读:
    27. Remove Element
    26. Remove Duplicates from Sorted Array
    643. Maximum Average Subarray I
    674. Longest Continuous Increasing Subsequence
    1. Two Sum
    217. Contains Duplicate
    448. Find All Numbers Disappeared in an Array
    566. Reshape the Matrix
    628. Maximum Product of Three Numbers
    UVa 1349 Optimal Bus Route Design (最佳完美匹配)
  • 原文地址:https://www.cnblogs.com/ad-zhou/p/9092941.html
Copyright © 2011-2022 走看看