2018--5-21 批量插入大数据的一点优化,小白一个

使用JDBC插入大量数据时,约20w条insert语句,最开始的代码:

Connection conn = null;
        Statement st = null;
        try{
            conn = dba.getConnection();
            st = conn.createStatement();
            long start = System.currentTimeMillis();
            if (batchSql.endsWith("/r/n"))
            {
                batchSql.subSequence(0, batchSql.length() - 2);
            }
            String[] datas = batchSql.split(m_strLines);
            for(int i=0;i<datas.length;i++){
                st.addBatch(datas[i]);
                if(i%4000 == 0){
                    st.executeBatch();
                   
                }
            }
            st.executeBatch();
           
            logger.info("-----------------------批量执行sql所需时间为:--"+(System.currentTimeMillis()-start)+"   毫秒");
        }catch(Exception e){
            logger.info("------------------------>批量执行sql方法出错了"+e);
        }finally{
            dba.close(st);
            dba.close(conn);

        }

执行的时间超乎想象,在网上查找优化方法,

加上conn.setAutoCommit(false);,在每次批量执行完提交一次,20万的数据仅仅用了37s

猜你喜欢

转载自blog.csdn.net/scanner_new/article/details/80388360