批量插入数据之程序优化
来源:互联网 发布:孤单北半球 知乎 编辑:程序博客网 时间:2024/06/07 23:49
最近遇到一个问题:我的程序需要向某张表中导入一万条数据,如何提高其效率?
在代码层面,下面两种方式是比较常见的:
使用Statement对象
使用PreparedStatement对象
执行插入的代码如下:
public void insertByStatement(String userName,String password){Connection conn=BaseDao.getInstance().getConnection();Statement statement=null;try {statement=conn.createStatement();String sql="insert into user_info (user_name,user_pass) values('"+userName+"',"+"'"+password+"')";int i=statement.executeUpdate(sql);if(i<=0){throw new SQLException("插入失败!");}} catch (SQLException e) {e.printStackTrace();}finally{BaseDao.getInstance().closeAll(null, statement, null);}}public void insertByPreparedStatement(String userName,String password){Connection conn=BaseDao.getInstance().getConnection();PreparedStatement ps=null;try {String sql="insert into user_info (user_name,user_pass) values(?,?)";ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);for (int i = 0; i < 2500; i++) {ps.setString(1, userName);ps.setString(2, password);ps.addBatch();}ps.executeBatch();} catch (SQLException e) {e.printStackTrace();}finally{BaseDao.getInstance().closeAll(null, ps, null);}}
下面分为单线程和多线程两种思路来测试:
单线程情况:
使用statement耗时218s。
try {conn.setAutoCommit(false);for(int i=1;i<=10000;i++){new PostsDao().insertByStatement("test", "test");}conn.commit();} catch (SQLException e) {try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}
使用preparedstatement批处理耗时8s。
try {conn.setAutoCommit(false);new PostsDao().insertByPreparedStatement("lemon", "lemon");conn.commit();} catch (SQLException e) {try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}
多线程(4个线程,每个线程插入2500条)情况下:使用Statement耗时61s,本文略去代码。
使用PreparedStatement批处理耗时4s,代码如下:
try {conn.setAutoCommit(false);ThreadPoolExecutor threadPool2 = new ThreadPoolExecutor(4, 4, 0,TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());threadPool2.execute(new ExecuteCallableThread());threadPool2.shutdown(); //关闭后不能加入新线程,队列中的线程则依次执行完while(threadPool2.getPoolSize()!=0);System.out.println("main thread end!");} catch (SQLException e) {try {conn.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}
本文仅仅在代码层面做了优化,可以看出,在一次性大量地插入数据时,PreparedStatement对象的executeBatch()和多线程的效率是比较好的。
- 批量插入数据之程序优化
- 批量插入数据优化
- hibernate批量数据插入优化
- MySQL 批量插入数据优化
- mysql 批量插入数据 优化
- mysql 批量插入数据优化
- MySql批量插入数据优化
- 批量插入数据之nologging
- 批量插入数据之nologging
- Yii2之批量插入数据
- postgresql优化数据的批量插入
- .NET 批量插入数据,先查后插,性能优化
- Android批量插入数据性能优化
- anroid Sqlite批量插入数据优化方法
- sqlite批量插入数据优化(转载)
- MySql海量数据批量插入优化小结
- sqlite批量插入数据性能优化
- MSSQL数据批量插入优化详细
- 【Android】Activity遮罩效果的实现
- 【笔面大全】随机数的相关面试题
- 用户级线程和内核级线程的区别
- IIS的搭建
- linux系统磁盘分区之parted
- 批量插入数据之程序优化
- practical model-based testing Written by Mark Utting & Bruno Legeard[preface]
- 产品黏度和用户体验
- 迅雷7 V 7.9.10.4610 Ayu 最新去广告纯净安装版
- 迅雷7 V 7.9.10.4610 Ayu 最新去广告纯净安装版
- 在Android中使用Handler和Thread线程执行后台操作
- 初识windows编程之二
- linux 系统磁盘分区之fdisk
- CStockPropImpl