資料庫效能提升之減少訪問資料庫次數

來源:互聯網
上載者:User

前面兩個方法我們通過調用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,這樣對資源也是一種浪費.

通常對兩種技術進行效能分析,唯一正確的方法是設定程式常見的情境來進行壓力測試.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.