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

您还没有登录,请登录后发表评论

heweiya
搜索本博客
我的相册
92e28bd8-89d3-39dd-990d-994641352b13-thumb
mvn-project.jpg
共 37 张
最近加入圈子
存档
最新评论