|
从网上找到资料,原来对于大数据量的分页,都不用skip+limit的,是通过改变查询规则来起到分页的效果。由于不用略过,所以无聊数据量有多大,时间复杂度的波动不大。
这个是根据,实现原理是通过数据库主键ID来进行查询分页
//操作数据库帮助类
protected MongoDBConnector_Local_NoAuth conn = new MongoDBConnector_Local_NoAuth();
//存放分页连对应主键ID,然后通过ID进行查询
protected Dictionary docId = new Dictionary();
///
/// 获取数据并进行分页计算
///
/// 这个用户查询
/// 分页大小
/// 当前页数
/// 分页的页面:比如Default.aspx
/// 这个是输出参数,输出分页连
/// 查询到数据
public IList GetList(string t, int PageSize, int CurrentPageIndex, string PageLink, out string homepage)
{
//获取数据库
MongoDatabase database = conn.DataBase_Select_ReturnDB("Ask");
//获取数据
MongoCollection mcollection = database.GetCollection("Answer");
QueryConditionList query = null;//定义查询条件
int id = 0;
object obj = HttpContext.Current.Session["ids"];//读取缓存数据
//缓存为空,重新开始查询
if (object.Equals(obj, null))
{
query = Query.GT("ID", 0);
}//分页连处理
else if (CurrentPageIndex ==1)
{
query = Query.GT("ID", 0);
}
else
{//读取条件生成查询
Dictionary docKey = obj as Dictionary;
id = docKey[CurrentPageIndex];
query = Query.GT("ID", id);
}
int recordCount = Convert.ToInt32(mcollection.Count());
int mpage = recordCount / PageSize + (recordCount % PageSize > 0 ? 1 : 0);
int PageNo = Math.Min(Convert.ToInt32(start) / PageSize + 1, mpage);
homepage = PageLink_aspx(PageLink, t, CurrentPageIndex, recordCount, mpage);
MongoCursor mcursor = mcollection.Find(query).SetLimit(20).SetSortOrder(SortBy.Ascending("ID"));
//判断1到6页 不变
if (CurrentPageIndex - 6 =0; i--)
{
start_left = start_left + 1;
docId.Add(start_left, alist_left[i * 20].ID);
}
int ys_left = cnt_left % 20;
if (ys_left != 0)
docId.Add(start_left + 1, alist_left[cnt_left - ys_left].ID);
//获取分页连右边数据
query = Query.GT("ID", id);
IList alist_right = mcollection.Find(query).SetLimit(120).SetSortOrder(SortBy.Ascending("ID")).ToList();
int start_right = start_left;
int cnt_right = alist_right.Count;
int page_right = cnt_right / 20;
for (int i = 0; i < page_right; i++)
{
if (i == 0)
continue;
start_right = start_right + 1;
docId.Add(start_right, alist_right[i * 20].ID);
}
int ys_right = cnt_left % 20;
if (ys_left != 0)
docId.Add(start_right + 1, alist_right[cnt_right - ys_right].ID);
HttpContext.Current.Session["ids"] = docId;
}
return mcursor.ToList();
}
///
/// 动态分页链接
///
/// 分页页面名称
/// 查询条件做URL地址进行传参
/// 当前页数
/// 总记录数
/// 总页数
///
public string PageLink_aspx(string HomePageStr, string t, int PageNo, int totalCnt, int mpage)
{
int i, j, k;
string homelink;
string homepage;
homepage = "";
homelink = HomePageStr
;
homepage = homepage + "第 " + (PageNo).ToString() + " / " + mpage.ToString() + " 页 共" + totalCnt + "条 ";
if (PageNo > 1)
{
homepage += " 首页 ";
homepage = homepage + " 上一页 ";
}
else { homepage = homepage + " 上一页 "; }
if (PageNo mpage) { k = mpage; }
for (j = i; j |
|
|