用Hibernate更新不到9万条数据用了2.5小时
来源:互联网 发布:安装ubuntu 安装类型 编辑:程序博客网 时间:2024/06/10 03:48
最近与六人行合作,给六人行增加新的功能,但又要照顾好原有的数据,我真的是伤透了脑筋。原来的基于asp下的数据库的设计简直一团糟,但你又不能大幅度 修改,以免对原有的asp程序产生重大的影响。由于缺乏系统的规划,而且双方沟通也有障碍,再加之我在整合旧系统方面确实缺乏足够的经验,这项工作进展非 常缓慢,我心里自然很是郁闷。
昨天从旧系统中涉及图片的部分,都抽象成了相册对象、图片对象和缩略图对象,并把原来的旧系统中的活动图片表里的数据用select into导到了新表中,不到9万条记录。但表中图片名需要处理一下,即更新9万条数据。我写了一段程序,计算出共多少记录,然后定好了每次取多少记录,计 算出取的次数,然后做了个循环,直接用hibernateDao的saveOrUpdate方法逐条更新,中间没有复杂的算法,但在我的机器(台式机, 3.0cpu,512M内存)上竟然用了2.5小时,感觉时间是不是长了点?把代码贴在下面,有感兴趣的请说教说教
昨天从旧系统中涉及图片的部分,都抽象成了相册对象、图片对象和缩略图对象,并把原来的旧系统中的活动图片表里的数据用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();
}
* 此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();
}
- 用Hibernate更新不到9万条数据用了2.5小时
- 极限挑战—C#+ODP 100万条数据导入Oracle数据库仅用不到1秒
- 极限挑战—C#+ODP 100万条数据导入Oracle数据库仅用不到1秒
- 极限挑战—C#+ODP 100万条数据导入Oracle数据库仅用不到1秒
- 极限挑战—C#+ODP 100万条数据导入Oracle数据库仅用不到1秒
- innodb优化后,29小时入库1300万条数据
- Oracle批量导入100万条数据不到1秒
- hibernate批量数据插入优化 ------用Hibernate插入1万条数据,怎么做,有什么好的优化方法吗
- 腾讯空间和朋友开放平台审核完毕,用了不到三个小时
- Hibernate 查询不到数据
- Hibernate 查询不到数据
- 10台PC 2亿数据量任意查询不到0.3秒, 插入 650万条数据仅用时 3分钟
- 我用Python爬了12万条影评,告诉你《战狼Ⅱ》都在说些啥
- 我用Python爬了12万条影评,告诉你《战狼Ⅱ》都在说些啥
- 插入10万条数据
- 处理上百万条数据
- 大众点评网全国数据2015年08月份更新(1480万条)
- 使用hibernate在5秒内插入11万条数据,你觉得可能吗?
- htmlarea下空格会变成乱码请升级htmlarea最终版!
- 教你一招从此下载CSDN资源不扣积分
- ForeignKeyConstraint外键约束的使用
- 今天微软的新品发布大会,没去真的很遗憾呀。
- Struts开发技巧(精彩)
- 用Hibernate更新不到9万条数据用了2.5小时
- asterisk App-Hangup 挂机
- java.lang.UnsupportedClassVersionError: xjavadoc/ant/XJavadocTask 解决办法
- 网络经典命令行
- 我的博客在百度:http://hi.baidu.com/sunmoonzhangli
- 批处理获取文件权限
- Java精华积累:每个初学者都应该搞懂的问题
- 检测程序中的括号是否匹配
- 被延迟的快乐不会产生利息