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
所以在进行第三方库的插入,最好选择效率最高的,而且在批量执行的时候,最好不要有数据冲突,否则执行失败,所以与业务无关的主键很重要.
原文地址:https://www.cnblogs.com/fxust/p/8119638.html