NBear的默认分页与自定义分页的比较

1.使用的数据库: Access数据库查询练习专用数据库 -手机号码归属地Access数据库_MobileDB(10万条记录)
2.两种分页算法
(1)NBear的默认分页方法;
         默认是使用了效率最差的方法:
None.gif SELECT   TOP   @PageSize   *   FROM   @TableName  
None.gif
WHERE   @PrimaryKey   NOT   IN  ( 
None.gif
SELECT   TOP   @PageSize * @PageIndex   @PrimaryKey   FROM   @TableName  
None.gif
ORDER   BY   @PrimaryKey   ASC  
None.gif
ORDER   BY   @PrimaryKey   ASC  
None.gif

(2) 黎波DataGrid基于Access的快速分页法
具体请查看DataGrid基于Access的快速分页法

3.结果分析:

各变量声明:
RecordCount :总记录数;
PageSize = 10 ,分页数;
PageNo :所要查询的页码;
PageTotal = (RecordCount / PageSize + RecordCount % PageSize == 0 ? 0 : 1) :总页数;
Middle =PageTotal / 2 :中间页码;

(1)PageNo <= 1


由结果可以看到,对于PageNo=1的情形,两种算法的查询时间相同,时间复杂度都为1.
对于自定义的快速分页法来说,在第一次加载绑定时,首先需要查询记录的总条数,不过,这所消耗的时间非常少。

(2)1< PageNo<= Middle
此时,对于10万条记录的Access数据库来讲,使用NBear的默认分页查询方式,已经无法运行,我等待了几分钟都还没出结果,就取消了。
以下看到的是自定义分页查询的结果。




(3)Middle < PageNo < PageTotal


(4)PageNo >= PageTotal



4.结论

1.这个自定义分页查询算法是很快速的, 假设把所有分页面划分为前面、中间和后面三部分,则最前面和最后面的分页速度最快,最中间的分页速度最慢。并且保证每个部分的速度均匀,造成速度随页码增大而变慢。

2.目前NBear的分页方法还是不高效的,如果项目中需要用到NBear进行大量数据分页查询,推荐使用NBear自定义方式;

3.对于Access数据库,因为不可以使用存储过程,所以只能动态创建Sql脚本来实现,通常这类分页的方法是类似的,可以写一个AccessPagingHelper 类来帮助我们重用自定义快速分页方法。
ContractedBlock.gif ExpandedBlockStart.gif AccessPagingHelper
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Text;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <summary>
InBlock.gif
/// AccessPagingHelper 的摘要说明  ,参考了NBear源代码中AccessStatementFactory.cs
ExpandedBlockEnd.gif
/// </summary>

None.gifpublic class AccessPagingHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public AccessPagingHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static String QuickPaging(int pageSize, int pageNo, int recordCount, string tableName, string primaryKey, bool isAsc, string whereStr, params string[] includeColumns)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (includeColumns.Length == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (whereStr.IndexOf('='> -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            whereStr 
= " WHERE " + whereStr;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            whereStr 
= "";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        StringBuilder sb 
= new StringBuilder();
InBlock.gif        sb.Append(
"SELECT ");
InBlock.gif
InBlock.gif        tableName 
= GetFormatValue(tableName);
InBlock.gif        primaryKey 
= GetFormatValue(primaryKey);
InBlock.gif        
string columns = GetColumns(includeColumns);
InBlock.gif        
string tempTableName1 = "tempTable1";
InBlock.gif        
string tempTableName2 = "tempTable2";
InBlock.gif
InBlock.gif        
int pageCount = GetPageCount(pageSize, recordCount);
InBlock.gif        
int middlePageNo = GetMiddlePageNo(pageCount);
InBlock.gif        
if (pageNo <= middlePageNo)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sb.Append(
"TOP ").Append(pageSize + " ").Append(columns);
InBlock.gif            sb.Append(
" FROM ").Append(tableName);
InBlock.gif            
if (pageNo <= 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
" WHERE ").Append(primaryKey).Append(isAsc ? ">(" : "<(");
InBlock.gif                sb.Append(
" SELECT ").Append(isAsc ? "MAX(" : "MIN(").Append(primaryKey).Append(") FROM (");
InBlock.gif                sb.Append(
" SELECT TOP ").Append(pageSize * pageNo).Append(" ").Append(primaryKey);
InBlock.gif                sb.Append(
" FROM ").Append(tableName);
InBlock.gif                sb.Append(whereStr);
InBlock.gif                sb.Append(
" ORDER BY ").Append(primaryKey).Append(isAsc ? " ASC" : " DESC");
InBlock.gif                sb.Append(
" ) AS ").Append(tempTableName1).Append(" )");
ExpandedSubBlockEnd.gif            }

InBlock.gif            sb.Append(whereStr);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sb.Append(
"* FROM (");
InBlock.gif            sb.Append(
"SELECT TOP ");
InBlock.gif            
if (pageNo < pageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(pageSize).Append(
" ").Append(columns);
InBlock.gif                sb.Append(
" FROM ").Append(tableName);
InBlock.gif                sb.Append(
" WHERE ").Append(primaryKey).Append(!isAsc ? ">(" : "<(");
InBlock.gif                sb.Append(
" SELECT ").Append(!isAsc ? "MAX(" : "MIN(").Append(primaryKey).Append(") FROM (");
InBlock.gif                sb.Append(
" SELECT TOP ").Append(recordCount - pageSize * pageNo).Append(" ").Append(primaryKey);
InBlock.gif                sb.Append(
" FROM ").Append(tableName);
InBlock.gif                sb.Append(whereStr);
InBlock.gif                sb.Append(
" ORDER BY ").Append(primaryKey).Append(!isAsc ? " ASC" : " DESC");
InBlock.gif                sb.Append(
" ) ").Append(tempTableName1).Append(")");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(recordCount 
- pageSize * (pageCount - 1)).Append(" ").Append(columns);
InBlock.gif                sb.Append(
" FROM ").Append(tableName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            sb.Append(whereStr);
InBlock.gif            sb.Append(
" ORDER BY ").Append(primaryKey).Append(!isAsc ? " ASC" : " DESC");
InBlock.gif            sb.Append(
") AS ").Append(tempTableName2);
ExpandedSubBlockEnd.gif        }

InBlock.gif        sb.Append(
" ORDER BY ").Append(primaryKey).Append(isAsc ? " ASC" : " DESC");
InBlock.gif
InBlock.gif        
return sb.ToString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static string GetFormatValue(string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (value[0== '[')
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return value;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return "[" + value + "]";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static string GetColumns(params string[] includeColumns)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        StringBuilder sb 
= new StringBuilder();
InBlock.gif        
int i = 1;
InBlock.gif        
foreach (string item in includeColumns)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (item[0== '*')
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (item[0== '[')
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(item);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
'[');
InBlock.gif                sb.Append(item);
InBlock.gif                sb.Append(
']');
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (i != includeColumns.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
',');
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            i
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return sb.ToString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static int GetPageCount(int pageSize, int recordCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return recordCount / pageSize + (recordCount % pageSize == 0 ? 0 : 1);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static int GetMiddlePageNo(int pageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return pageCount / 2;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

5.附
我的测试 源文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值