zoukankan      html  css  js  c++  java
  • Mysql批量插入executeBatch测试

    Mysql批量插入executeBatch测试

    第一次:没有设置手动提交,jdbc自动提交,代码如下:

    public void test23(){
    		Connection conn = new ConnectionManager().newConnection();
            PreparedStatement ps = null;
            try {
                String sql = "insert into uuidtest(uuid) values(?)";
                ps = conn.prepareStatement(sql);
                long start = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    String uuid = UUID.randomUUID().toString();
                    System.out.println(uuid);
                    ps.setString(1, uuid);
                    ps.addBatch();
                }
                ps.executeBatch();
                long end = System.currentTimeMillis();
                System.out.println(end-start);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
    	}
    

    执行时间为:165766毫秒

    第二次:设置手动提交,程序最后手动提交,代码如下:

    public void test23(){
    		Connection conn = new ConnectionManager().newConnection();
            PreparedStatement ps = null;
            try {
            	conn.setAutoCommit(false);
                String sql = "insert into uuidtest(uuid) values(?)";
                ps = conn.prepareStatement(sql);
                long start = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    String uuid = UUID.randomUUID().toString();
                    System.out.println(uuid);
                    ps.setString(1, uuid);
                    ps.addBatch();
                }
                ps.executeBatch();
                conn.commit();
                long end = System.currentTimeMillis();
                System.out.println(end-start);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
    	}
    

    执行时间为:1135毫秒

    第三次:设置手动提交,程序最后手动提交,每一千次执行缓存的sql语句并清空缓存,代码如下:

    public void test23(){
    		Connection conn = new ConnectionManager().newConnection();
            PreparedStatement ps = null;
            try {
            	conn.setAutoCommit(false);
                String sql = "insert into uuidtest(uuid) values(?)";
                ps = conn.prepareStatement(sql);
                long start = System.currentTimeMillis();
                for (int i = 0; i < 10000; i++) {
                    String uuid = UUID.randomUUID().toString();
                    System.out.println(uuid);
                    ps.setString(1, uuid);
                    ps.addBatch();
                    if(i%1000==0){
                    	ps.executeBatch();
                    	ps.clearBatch();
                    }
                }
                conn.commit();
                long end = System.currentTimeMillis();
                System.out.println(end-start);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
    	}
    

    执行时间为:993毫秒

  • 相关阅读:
    Jenkins配置钉钉通知
    Jenkins 学习笔记
    2020年10月26日Britain suggests it may overturn parts of the EU withdrawal agreement
    【火爆抢答中】HarmonyOS有奖问答,更多惊喜等你来拿!
    三七互娱《斗罗大陆:魂师对决》上线,Network Kit助力玩家即刻畅玩
    运动健康南向设备接入服务传输数据解析举例
    华为商品管理系统批量更新商品时提示:请至少输入一组国家码和价格
    云空间服务,助力用户数据存储与协同
    Input组件无点击效果
    华为视频编辑服务(Video Editor Kit),助力开发者高效构建应用视频编辑能力
  • 原文地址:https://www.cnblogs.com/jimloveq/p/10609490.html
Copyright © 2011-2022 走看看