zoukankan      html  css  js  c++  java
  • JDBC插入性能优化对比

    今天对Insert进行了性能测试,结果反差很大,平时都是单条插入,虽然性能要求没有那么高,但是突然在项目中,人家给定时间内完成,这就尴尬了.

    优化数据库,优化服务器,优化代码,反正通过各种优化提高数据的处理速度.

    接下来对jdbc插入做一个测试,测试代码入如下:

        /**
         * 生成插入语句
         * @author fgq 2017年12月26日 下午6:40:03
         * @return
         */
        public static List<String> getInsertSql(){
            String sqlModel = "insert into test_cost1(id,name)values('{id}','{name}')";
            List<String> insertSqls = new ArrayList<String>();
            for(int i=0;i<10000;i++){
                sqlModel = sqlModel.replace("{id}", UUIDUtil.getRandomUUID());
                sqlModel = sqlModel.replace("{name}", "danny");
                insertSqls.add(sqlModel);
            }
            return insertSqls;
        }
        /**
         * Statement单条插入
         * @author fgq 2017年12月26日 下午6:39:05
         * @param conn
         * @throws Exception
         */
        public static void executeSQL(Connection conn) throws Exception {        
            List<String> insertSql = getInsertSql();
            Statement stmt = conn.createStatement();
            for(String sql : insertSql){
                stmt.execute(sql);
            }
        }
        
        /**
         * Statement批量插入
         * @author fgq 2017年12月26日 下午6:39:24
         * @param conn
         * @throws Exception
         */
        public static void executeBatchSQL(Connection conn) throws Exception {
            List<String> insertSql = getInsertSql();
            Statement stmt = conn.createStatement();
            for(String sql : insertSql){
                stmt.addBatch(sql);
            }
            stmt.executeBatch();
        }
        /**
         * PreparedStatement批量插入
         * @author fgq 2017年12月26日 下午6:40:34
         * @param conn
         * @throws Exception
         */
        public static void batchInsertData(Connection conn) throws Exception{
            String prefix = "insert into test_cost1(id,name)values(?,?)";
            PreparedStatement  pst = conn.prepareStatement(prefix);
                for(int j = 0;j<10000;j++){
                    pst.setString(1, UUIDUtil.getRandomUUID());
                    pst.setString(2, "liming");
                    pst.addBatch();
                }
                pst.executeBatch();
        }
        
        /**
         * PreparedStatement单条插入
         * @author fgq 2017年12月26日 下午6:40:16
         * @param conn
         * @throws Exception
         */
        public static void insertData(Connection conn) throws Exception{
            String prefix = "insert into test_cost1(id,name)values(?,?)";
            PreparedStatement  pst = conn.prepareStatement(prefix);
                for(int j = 0;j<10000;j++){
                    pst.setString(1, UUIDUtil.getRandomUUID());
                    pst.setString(2, "liming");
                    pst.executeUpdate();
                }
        }
        public static void main(String[] args) throws Exception {        
            final String url = "jdbc:oracle:thin:@123.123.123.123:1521/orcl"; 
            final String name = "oracle.jdbc.driver.OracleDriver"; 
            final String user = "test"; 
            final String password = "test"; 
            Connection conn = null; 
            Class.forName(name);//指定连接类型 
            conn = DriverManager.getConnection(url, user, password);//获取连接 
            if (conn!=null) {
                System.out.println("获取连接成功");
                long startTime = System.currentTimeMillis();
                insertData(conn);
                System.out.println("执行1000插入耗时:"+(System.currentTimeMillis() - startTime));
            }else {
                System.out.println("获取连接失败");
            }
        }

    通过上面10000条测试结果,发现效率最高的是

    batchInsertData

    最慢的是

    insertData
    所以在进行第三方库的插入,最好选择效率最高的,而且在批量执行的时候,最好不要有数据冲突,否则执行失败,所以与业务无关的主键很重要.
  • 相关阅读:
    atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
    atitit.查看预编译sql问号 本质and原理and查看原生sql语句
    atitit.基于http json api 接口设计 最佳实践 总结o7
    atitit.spring3 mvc url配置最佳实践
    Atitit.列表页面and条件查询的实现最佳实践(2)翻页 分页 控件的实现java .net php
    atitit。自定义uml MOF EMF体系eclipse emf 教程o7t
    atitit.编辑表单的实现最佳实践dwr jq easyui
    Atitit. 提升开发效率与质量DSL ( 3) 实现DSL的方式总结
    atitit.设计模式(2) 查表模式/ command 总结
    Atitit. 提升软件开发效率and 开发质量java 实现dsl 4gl 的本质and 精髓 O725
  • 原文地址:https://www.cnblogs.com/fxust/p/8119638.html
Copyright © 2011-2022 走看看