用Hibernate更新不到9万条数据用了2.5小时

来源:互联网 发布:安装ubuntu 安装类型 编辑:程序博客网 时间:2024/06/10 03:48
最近与六人行合作,给六人行增加新的功能,但又要照顾好原有的数据,我真的是伤透了脑筋。原来的基于asp下的数据库的设计简直一团糟,但你又不能大幅度 修改,以免对原有的asp程序产生重大的影响。由于缺乏系统的规划,而且双方沟通也有障碍,再加之我在整合旧系统方面确实缺乏足够的经验,这项工作进展非 常缓慢,我心里自然很是郁闷。
昨天从旧系统中涉及图片的部分,都抽象成了相册对象、图片对象和缩略图对象,并把原来的旧系统中的活动图片表里的数据用select into导到了新表中,不到9万条记录。但表中图片名需要处理一下,即更新9万条数据。我写了一段程序,计算出共多少记录,然后定好了每次取多少记录,计 算出取的次数,然后做了个循环,直接用hibernateDao的saveOrUpdate方法逐条更新,中间没有复杂的算法,但在我的机器(台式机, 3.0cpu,512M内存)上竟然用了2.5小时,感觉时间是不是长了点?把代码贴在下面,有感兴趣的请说教说教 
    /**
     * 此action处理表的图片的url,在最初由旧表导入数据时,url字段里只有图片的名称,<br>
     * 没有存储目录,我们此方法就是把图片对应的目录也给加上。加目录的规则是,取图片<br>
     * 上传时间的年,月,组成目录名,然后再和图片名,一起构成图片的url.当然,此url<br>
     * 相对于$req.contextPath/uploadFiles目录<br>
     * 使用演示:formatImageUrl.action?myUrl=success<br>
     * 
@author Davy Lee
     * 
@return String myUrl
     * 
@throws Exception
     
*/

    
public String formatImageUrl() throws Exception
    
{
        
int totalRecord = 0;
        
int totalPage = 0;
        
try 
        
{
            totalRecord 
= ((Integer)((ImageList)this.getTableList()).getOption().uniqueResult("select count(t.id) from TImage as t")).intValue();
        }
 catch (Exception e) 
        
{
            log.error(e);
        }

        
if(totalRecord % this.getPageSize() == 0)
        
{
            totalPage 
= totalRecord / this.getPageSize();
        }
else
        
{
            totalPage 
= totalRecord / this.getPageSize() + 1;
        }

        
if(totalPage > 0)
        
{
            List result 
= null;
            log.debug(
"一共有:" + totalPage + "页!");
            log.debug(
"每页记录数:" + this.getPageSize());
            log.debug(
"总共记录数为:" + totalRecord);
            
for(int i=1;i<(totalPage+1);i++)
            
{
                log.debug(
"开始处理第i页");
                
try 
                
{
                    
//fetch all record of TImage object
                    ((ImageList)this.getTableList()).setPageNum(i);
                    result 
= ((ImageList)this.getTableList()).getList("from TImage as t ");
                }
 catch (RuntimeException e) 
                
{
                    log.error(e);
                }

                
if(result != null && result.size() > 0)
                
{
                    ITImage image 
= null;
                    SimpleDateFormat yearFormat 
= new SimpleDateFormat("yyyy");
                    SimpleDateFormat monthFormat 
= new SimpleDateFormat("M");
                    
//year of the upload time
                    String year = "";
                    
//month of the upload time
                    String month = "";
                    
//upload time
                    Date inTime = null;
                    
//the name of the picture
                    String picName = "";
                    
//the final url of picture which saved in database
                    String url = "";
                    log.debug(
"be careful!It is time to start ........");
                    
for(int j=0;j<result.size();j++)
                    
{        
                        log.debug(
"开始处理第" + i + "页的第" + j + "条记录!");
                        image 
= (ITImage) result.get(j);
                        log.debug(
"current id is :" + image.getId());
                        inTime 
= image.getInTime();
                        year 
= yearFormat.format(inTime);
                        month 
= monthFormat.format(inTime);
                        picName 
= image.getUrl();
                        
//calculate final url
                        url = year + "-" + month + "/" + picName;
                        image.setUrl(url);
                        
//save object
                        ((ImageList)this.getTableList()).getOption().saveOrUpdate(image);
                        
//clear variables
                        image = null;
                        inTime 
= null;
                        year 
= "";
                        month 
= "";
                        picName 
= "";
                        url 
= "";
                        log.debug(
"结束处理第" + i + "页的第" + j + "条记录!");
                    }

                    log.debug(
"be careful!It is time to end ........");
                    result 
= null;
                }

            }
        
        }

        
return this.getMyUrl();        
    }