前面兩個方法我們通過調用ProductsBLL類的GetProductsByCategoryID(categoryID)方法來擷取當前 category的product(第一種通過ObjectDataSource,第二種通過GetProductsInCategory (categoryID)).每次方法被調用時,BLL調用DAL,DAL通過SQL查詢資料庫,返回特定的記錄.
如果有N個category,這個方法會訪問資料庫N+1次— 一次返回所有的category,N次返回特定category下的product.然而我們可以通過訪問資料庫兩次來擷取所有需要的資料— 一次返回所有的category,一次返回所有的product.一旦我們得到所有的product,我們可以根據CategoryID來過濾,然後再綁 定.
我們只需要稍微修改ASP.NET頁的code-behind裡的GetProductsInCategory(categoryID)方法來實現這個功能.我們首先來返回所有的product,然後根據傳入的CategoryID裡過濾.
private Northwind.ProductsDataTable allProducts = null;
protected Northwind.ProductsDataTable GetProductsInCategory(int categoryID)
...{
// First, see if we've yet to have accessed all of the product information
if (allProducts == null)
...{
ProductsBLL productAPI = new ProductsBLL();
allProducts = productAPI.GetProducts();
}
// Return the filtered view
allProducts.DefaultView.RowFilter = "CategoryID = " + categoryID;
return allProducts;
}
注意allProducts變數.它在第一次調用GetProductsInCategory(categoryID)時返回所有 product資訊.確定allProducts對象被建立後,在根據CategoryID來對DataTable過濾.這個方法將訪問資料庫的次數從N +1減少到2次.
這個改進沒有修改頁面的聲明語言.僅僅只是減少了資料庫的訪問次數.
注意:可能想當然的覺得減少了資料庫訪問次數會提高效能.但是這個不一定.如果你有大量的categoryID為NULL的product,這樣使 用GetProducts方法返回的product有一部分不會被顯示.而且如果你只需要顯示一部分category的proudct(分頁時就是這 樣),而返回所有的product,這樣對資源也是一種浪費.
通常對兩種技術進行效能分析,唯一正確的方法是設定程式常見的情境來進行壓力測試.