1. 程式人生 > >for迴圈裡面進行資料庫查詢弊端

for迴圈裡面進行資料庫查詢弊端

for迴圈裡面沒有dao的查詢,方法一:

List<ProductPop> list = productDao.getProudctList(map);

if (list.size() > 0) {
Map<String, Object> map2 = new HashMap<String, Object>();
List<String> proList =new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
ProductPop productPop = list.get(i);
proList.add(productPop.getProduct_id());
if (searcher!=null) {
map2.put("status", searcher.getStatus());
}
}
map2.put("products_id",proList);
List<GoodsPop> goodsPop = productDao.queryGoodsList(map2);
for (int i = 0; i < list.size(); i++) {
ProductPop productPop = list.get(i);
List<GoodsPop> goodsPopList=new ArrayList<GoodsPop>();
for (GoodsPop goodsPop2 : goodsPop) {
if (productPop.getProduct_id().equals(goodsPop2.getProducts_id())) {
goodsPopList.add(goodsPop2);
}
}
productPop.setGoodsList(goodsPopList);
}
}
return list;

}

for迴圈裡面有新增dao進行資料庫查詢,方法二:

List<ProductPop> list = productDao.getProudctList(map);
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
ProductPop productPop = list.get(i);
Map<String, String> map2 = new HashMap<String, String>();
map2.put("products_id", productPop.getProduct_id());
if (searcher!=null) {
map2.put("status", searcher.getStatus());
}
List<GoodsPop> goodsPop = productDao.queryGoods(map2);
if (goodsPop!=null&&goodsPop.size()>0) {
for (int j = 0; j < goodsPop.size(); j++) {
GoodsPop gPop=goodsPop.get(j);
Integer showQuality = 0;
if (gPop.getSku()!=null&&!"".equals(gPop.getSku())) {
List<AtpQueryStock> gStockList = productDao.queryGoodsStockByGidAndWhId(gPop.getSku());
if(gStockList!=null&&gStockList.size()>0){
showQuality = new Integer(gStockList.get(0).getAvailableQty());
}
}
gPop.setShow_quantity(showQuality);
}
}

productPop.setGoodsList(goodsPop);
}
}

兩種書寫方式在本地小資料測試,效能都差不多,但是在大資料的生產環境測試的時候,方法一比方法二就有明顯的有事,

特別是在方法一比方法二減少了對資料庫的訪問,這樣就可以避免因為連線資料庫的中斷而出現問題,這樣就更加穩定。