博客
关于我
数据库批处理和一般处理的快慢比较
阅读量:635 次
发布时间:2019-03-14

本文共 3104 字,大约阅读时间需要 10 分钟。

 下面有两个方法:ct1方法是标准的处理模式,ct2方法是批处理的模式。

最终显示:批处理模式的速度比一般处理模式块的很多,在实际开发中,一个表的字段越多,一般二三十个很常见,SQL语句越复杂,越能体现出批处理的速度之快!

package com.wei.employee.common;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import java.util.Date;/** * 批处理模式 和一般处理模式下 比较之下 看谁的执行效率快 */public class BatchSample {    //标注模式下的处理模式    public static void tc1(){        Connection con=null;        PreparedStatement pstmt=null;        try {            long statrTime=new Date().getTime();            con = DbUtils.getConnection();            //JDBC默认使用自动提交的模式            con.setAutoCommit(false);//关闭自动提交模式            String sql="insert into employee(eno,ename,salary,dname)values(?,?,?,?)";            for (int i = 100000; i < 200000; i++) {//                if (i==1005){//                    throw  new Exception("插入失败");//                }                pstmt = con.prepareStatement(sql);                pstmt.setInt(1,i);                pstmt.setString(2,"员工"+i);                pstmt.setFloat(3,40000f);                pstmt.setString(4,"研发部");                pstmt.executeUpdate();            }            con.commit();//提交数据            long endTime=new Date().getTime();            System.out.println("tc1方法的执行总时长:"+(endTime-statrTime));        } catch (Exception e) {            e.printStackTrace();            try {                if (con!=null&&!con.isClosed()){                    con.rollback();//回滚数据                }            } catch (SQLException throwables) {                throwables.printStackTrace();            }        }finally {            DbUtils.closeConnection(null,pstmt,con);        }    }    //批处理模式下执行的的时间    public static void tc2(){        Connection con=null;        PreparedStatement pstmt=null;        try {            long statrTime=new Date().getTime();            con = DbUtils.getConnection();            //JDBC默认使用自动提交的模式            con.setAutoCommit(false);//关闭自动提交模式            String sql="insert into employee(eno,ename,salary,dname)values(?,?,?,?)";            pstmt = con.prepareStatement(sql);            for (int i = 200000; i < 300000; i++) {//                if (i==1005){//                    throw  new Exception("插入失败");//                }                pstmt.setInt(1,i);                pstmt.setString(2,"员工"+i);                pstmt.setFloat(3,40000f);                pstmt.setString(4,"研发部");                pstmt.addBatch();//将刚才设置的参数加入批处理的任务中//                pstmt.executeUpdate();            }            pstmt.executeBatch();//执行批处理任务            con.commit();//提交数据            long endTime=new Date().getTime();            System.out.println("tc2方法的执行总时长:"+(endTime-statrTime));        } catch (Exception e) {            e.printStackTrace();            try {                if (con!=null&&!con.isClosed()){                    con.rollback();//回滚数据                }            } catch (SQLException throwables) {                throwables.printStackTrace();            }        }finally {            DbUtils.closeConnection(null,pstmt,con);        }    }    public static void main(String[] args) {        tc1();        tc2();    }}

运行截图:

 

转载地址:http://ujooz.baihongyu.com/

你可能感兴趣的文章
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>