数据库性能提升之减少访问数据库次数
2007-10-15 14:18
513 查看
前面两个方法我们通过调用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,这样对资源也是一种浪费.
通常对两种技术进行性能分析,唯一正确的方法是设置程序常见的场景来进行压力测试.
如果有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,这样对资源也是一种浪费.
通常对两种技术进行性能分析,唯一正确的方法是设置程序常见的场景来进行压力测试.
相关文章推荐
- 数据库提升性能 要减少访问数据库次数
- 数据库性能提升之减少访问数据库次数
- 数据库性能提升之减少访问数据库次数
- 减少访问DOM的次数提升javascript性能
- javascript性能提升——减少访问DOM的次数
- 数据库性能优化-4-减少对数据库的连接次数
- 设计文件系统时应尽量减少访问磁盘的次数,以提高文件系统的性能.下列各种措施中,哪些可以减少磁盘服务时间?
- C#使用DataTable传递数据减少数据库访问打开关闭次数
- Oracle 减少数据库访问次数
- 【系统性能优化】减少页面对公共资源Easyui的访问次数
- 应用 memcached 提升站点性能——减少读自数据库和数据源
- 从减少装箱次数出发提升代码性能
- 如何减少对数据库的访问次数来加快sql执行
- 应用 memcached 提升站点性能——减少读自数据库和数据源
- ASP.NET站点性能提升-加速数据库访问
- 应用 memcached 提升站点性能——减少读自数据库和数据源
- 应用 memcached 提升站点性能——减少读自数据库和数据源
- 应用 memcached 提升站点性能——减少读自数据库和数据源
- 应用 memcached 提升站点性能——减少读自数据库和数据源