2007-11-09
优化hibernate的一招。
原来的程序:
呵呵。这是一个检查重复的产品型号的SQL,其中语法第4行:from Productinfo as b where b.productSpec....当中 “from Productinfo as b ”是一个很浪费内存的东东。
其中语法第9行:Productinfo info = (Productinfo) Lrep.get(j);也是一个很浪费内存的东东。
我的修改建议是:只把有用的字段取出来就行了,不需要兴师动众。真正有用的字段是:info.getId()
修改方法如下:
其中:String proid = Lrep.get(j).toString();能够加快读取好多倍,也省了很多内存的读写,以后把这个做为一个习惯就好了。一定要优化这类的编码呀!
java 代码
- List Lrep = this
- .getHibernateTemplate()
- .find(
- "from Productinfo as b where b.productSpec='"
- + spec[0].toString()
- + "' order by b.id");
- if (Lrep != null) {
- for (int j = 0; j < repeat+1; j++) {
- Productinfo info = (Productinfo) Lrep.get(j);
- /**
- * 如果有重复的产品型号,命名规则是:
- */
- if (j > 0) {
- if (info.getProductSpec().equals("*")||info.getProductSpec().equals("-")){
- System.out.println("update productinfo set LinkSpecFix='echo-"+info.getId()+ "' where id="+info.getId()+";--"+info.getProductSpec());
- }else{
- System.out.println("update productinfo set LinkSpecFix='"+info.getProductSpec()+ "-echo" + j+"' where id="+info.getId()+";--"+info.getProductSpec()+"--数"+i);
- }
- }
- }
- }
其中语法第9行:Productinfo info = (Productinfo) Lrep.get(j);也是一个很浪费内存的东东。
我的修改建议是:只把有用的字段取出来就行了,不需要兴师动众。真正有用的字段是:info.getId()
修改方法如下:
java 代码
- List Lrep = this
- .getHibernateTemplate()
- .find(
- "select b.id from Productinfo as b where b.productSpec='"
- + spec[0].toString()
- + "' order by b.id");
- if (Lrep != null) {
- for (int j = 0; j < repeat+1; j++) {
- String proid = Lrep.get(j).toString();//加快SQL检索
- /**
- * 如果有重复的产品型号,命名规则是:
- */
- if (j > 0) {
- if (spec[0].toString().equals("*")||spec[0].toString().equals("-")){
- System.out.println("update productinfo set LinkSpecFix='echo-"+proid+ "' where id="+proid+";--"+spec[0].toString());
- }else{
- System.out.println("update productinfo set LinkSpecFix='"+spec[0].toString()+ "-echo" + j+"' where id="+proid+";--"+spec[0].toString()+"--数"+i);
- }
- }
- }
- 10:20
- 浏览 (302)
- 评论 (0)
- 分类: STRUTS SPRING HIBERNATE
- 相关推荐
发表评论
- 浏览: 116520 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
mvn-project.jpg
共 37 张
共 37 张
最近加入圈子
最新评论
-
Netbeans VS Myeclipse和 ...
冷...Netbeans VS Myeclipse和自动档汽车 vs 手动档汽车 ...
-- by lordhong -
Netbeans VS Myeclipse和 ...
我也喜欢EMACS,JDE挺好用的,就是不知道怎么调试WEB程序
-- by lululau -
使用openfire+jwchat来构 ...
楼上只说了在线消息的发送代码,我想知道:离线消息是怎么保存的呢?
-- by jiahx -
使用openfire+jwchat来构 ...
ajax都存在一个跨域的问题,看apache是用url rewrite解决的,不 ...
-- by ayeah -
如何利用OSWorkflow的func ...
function和condition里的actionId可以通过transien ...
-- by fantasy






评论排行榜