我用的是PreparedStatement来发送提交,并且数据量非常庞大。
一次需要执行8900多条更新语句,也就是Update语句。
我试过分开,一部分一部分来执行提交,但还是一样的速度慢,而且越执行越慢。实在是没有什么好的办法了,请教各位高手。

解决方案 »

  1.   

    8900多条更新,会慢,那就是写法有问题!把SQL贴上来!
      

  2.   

    // 执行更新语句,将值插入到历史表中。
    @SuppressWarnings("unchecked")
    public void updateHistory(Map map, Measure measure) throws Exception {
    String sql = " update history" + measure.getH_tableid() + " set field"
    + measure.getH_fieldid() + "=? where id =?";
    System.out.println("map=" + map.size());
    frame.getJTextArea1().append("map=" + map.size() + "\n");
    frame.getInfoArea().append(sql + "\r\n");
    PreparedStatement pstm = null;
    int count = 0;
    try {
    conn.setAutoCommit(false);
    pstm = conn.prepareStatement(sql);
    Set set = map.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
    count++;
    long sttime = System.currentTimeMillis();
    Map.Entry entry = (Map.Entry) it.next();
    List values = (List) entry.getValue();
    for (int j = 1; j <= 12; j++) {
    String value = StringUtil.valueTranser(values.get(j - 1)
    + "");
    String id = DateUtil.calculateTime((Date) entry.getKey(),
    j * 5);
    pstm.setString(1, value);
    pstm.setString(2, id);
    pstm.addBatch();
    }
    System.out.println("耗时:"
    + (System.currentTimeMillis() - sttime) + "count="
    + count);
    pstm.executeBatch();
    }
    conn.commit();
    conn.setAutoCommit(true);
    this.frame.getInfoArea()
    .append(
    "测点" + measure.getName() + "成功执行操作!count=" + count
    + "\r\n");
    System.out.println("测点" + measure.getName() + "成功执行操作!");
    } catch (Exception e) {
    conn.rollback();
    e.printStackTrace();
    } finally {
    pstm.close();
    }
    }
      

  3.   

    public void updateHistory(Map map, Measure measure) throws Exception { 
       String sql = " update history" + measure.getH_tableid() + " set field" 
                      + measure.getH_fieldid() + "=? where id =?"; 
       //System.out.println("map=" + map.size()); 
       frame.getJTextArea1().append("map=" + map.size() + "\n"); 
       frame.getInfoArea().append(sql + "\r\n"); 
       PreparedStatement pstm = null; 
       int count = 0; 
       try { 
          conn.setAutoCommit(false); 
          pstm = conn.prepareStatement(sql); 
          Set set = map.entrySet(); 
          Iterator it = set.iterator(); 
          while (it.hasNext()) { 
             count++; 
             long sttime = System.currentTimeMillis(); 
             Map.Entry entry = (Map.Entry) it.next(); 
             List values = (List) entry.getValue(); 
             for (int j = 1; j <= 12; j++) { 
                //String value = StringUtil.valueTranser(values.get(j - 1) + ""); 
                //String id = DateUtil.calculateTime((Date) entry.getKey(), j * 5); 
                pstm.setString(1, StringUtil.valueTranser(values.get(j - 1) + "")); 
                pstm.setString(2, DateUtil.calculateTime((Date) entry.getKey(), j * 5)); 
                pstm.addBatch(); 
             } 
             //System.out.println("耗时:" + (System.currentTimeMillis() - sttime) + "count=" + count); 
             //pstm.executeBatch(); 
          } 
          pstm.executeBatch();
          conn.commit(); 
          conn.setAutoCommit(true); 
          this.frame.getInfoArea().append("测点" + measure.getName() + "成功执行操作!count=" + count + "\r\n"); 
          System.out.println("测点" + measure.getName() + "成功执行操作!"); 
       } catch (Exception e) { 
          conn.rollback(); 
          e.printStackTrace(); 
       } finally { 
          pstm.close(); 
       } 
    }StringUtil和DateUtil两个类也可以改进或者弃用。
      

  4.   

    shiyiwan谢谢你的建议,我想说,那两个类不能弃用,因为需要转换格式,所以必须得有,我具体观察过执行过程,时间都耗在了执行上,也就是pstm.executeBatch();上了。而且随着程序的运行,越往后,会越慢。
      

  5.   

    pstm.executeBatch();
    移到外面去了吗?
    换一个oracle driver试试,最好是最新的,14的那个jar,别用class12.jar了。
    查了下OraclePreparedStatement,没法知道里面executeBatch方法的具体实现。
      

  6.   

    嗯,移到外面试过好多次,都是越来越慢,开始速度还行,越运行越慢了。用的就是14的JAR包。
    是啊,我也用过OraclePreparedStatement效果一样。
    这个问题真是蹊跷啊,都好长时间了,一直没找到什么好方法。
    谢谢你了啊。
      

  7.   

    通过微软自带的基本工具看到的确内存有所增长,不过我的内存看来应该是够用的,不至于紧张。
    是不是可能是oracle的数据库缓存已经超支了?
      

  8.   

    你直接用PL/SQL DEVELOPER 执行下 看慢不慢 看看执行计划
    ORACLE 更新大表确实有问题!