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连接 不具备其他功能 然后测试可以使用批出指令的时间为

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

  • 相关阅读:
    Mybatis中#{}和${}传参的区别
    笔记摘抄 —— shiro学习篇
    使用Spring的Testcase的单元测试的写法
    【转】FreeMarker学习笔记
    破解Pycharm,IDEA,PhpStrom等系列产品的,有关JetbrainsCrack的使用方法
    Python的字符串
    python的变量
    python开头注释
    h5-动画小案例-滚动展示
    h5-钟表动画案例
  • 原文地址:https://www.cnblogs.com/ad-zhou/p/9092941.html
Copyright © 2011-2022 走看看