从网上找到资料,原来对于大数据量的分页,都不用skip+limit的,是通过改变查询规则来起到分页的效果。由于不用略过,所以无聊数据量有多大,时间复杂度的波动不大。
这个是根据,实现原理是通过数据库主键ID来进行查询分页
//操作数据库帮助类 protected MongoDBConnector_Local_NoAuth conn = new MongoDBConnector_Local_NoAuth(); //存放分页连对应主键ID,然后通过ID进行查询 protected Dictionary<int, int> docId = new Dictionary<int, int>(); /// <summary> /// 获取数据并进行分页计算 /// </summary> /// <param name="t">这个用户查询</param> /// <param name="PageSize">分页大小</param> /// <param name="CurrentPageIndex">当前页数</param> /// <param name="PageLink">分页的页面:比如Default.aspx</param> /// <param name="homepage">这个是输出参数,输出分页连</param> /// <returns>查询到数据</returns> public IList<Answer> GetList(string t, int PageSize, int CurrentPageIndex, string PageLink, out string homepage) { //获取数据库 MongoDatabase database = conn.DataBase_Select_ReturnDB("Ask"); //获取数据 MongoCollection<Answer> mcollection = database.GetCollection<Answer>("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<int, int> docKey = obj as Dictionary<int, int>; 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<Answer> mcursor = mcollection.Find(query).SetLimit(20).SetSortOrder(SortBy.Ascending("ID")); //判断1到6页 不变 if (CurrentPageIndex - 6 <= 0) { query = Query.GT("ID", 1); IList<Answer> alist = mcollection.Find(query).SetLimit(220).SetSortOrder(SortBy.Ascending("ID")).ToList<Answer>(); int cnt = alist.Count(); int index = 0; int page = cnt / 20; for (int i = 0; i < page; i++) { index = i + 1; docId.Add(index, alist[i * 20].ID); } int ys = cnt % 20; if (ys != 0) docId.Add(index + 1, alist[cnt - ys].ID); HttpContext.Current.Session["ids"] = docId; } else { //处理左边数据 docId.Clear(); query = Query.LTE("ID", id); IList<Answer> alist_left = mcollection.Find(query).SetLimit(120).SetSortOrder(SortBy.Descending("ID")).ToList<Answer>(); int start_left = CurrentPageIndex - 6; int cnt_left = alist_left.Count; int page_left = cnt_left / 20; for (int i = page_left-1; i>=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<Answer> alist_right = mcollection.Find(query).SetLimit(120).SetSortOrder(SortBy.Ascending("ID")).ToList<Answer>(); 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<Answer>(); } /// <summary> /// 动态分页链接 /// </summary> /// <param name="HomePageStr">分页页面名称</param> /// <param name="t">查询条件做URL地址进行传参</param> /// <param name="PageNo">当前页数</param> /// <param name="totalCnt">总记录数</param> /// <param name="mpage">总页数</param> /// <returns></returns> 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 += " <a href=\"" + homelink + "?start=1" + t + "\">首页</a> "; homepage = homepage + " <a href=\"" + homelink + "?start=" + Convert.ToString((PageNo - 1)) + t + "\">上一页 </a> "; } else { homepage = homepage + " <font color=\"#999999\" >上一页 </font> "; } if (PageNo <= 5) { i = 1; } else { i = PageNo - 5; } k = i + 10; if (k > mpage) { k = mpage; } for (j = i; j <= k; j++) { if (j == PageNo) { homepage = homepage + "<b><font color=\"#ff0000\" >" + Convert.ToString(j) + "</font></b> "; } else if (j == 1) { homepage = homepage + "<a href=\"" + homelink + "?start=1" + t + "\" >[" + Convert.ToString(j) + "]</a> "; } else { homepage = homepage + "<a href=\"" + HomePageStr + "?start=" + j + t + "\">[" + Convert.ToString(j) + "]</a> "; } } if (PageNo < mpage) { homepage = homepage + " <a href=\"" + HomePageStr + "?start=" + Convert.ToString((PageNo + 1)) + t + "\">下一页</a> "; homepage += " <a href=\"" + HomePageStr + "?start=" + mpage + t + "\">最后一页</a> "; } else { homepage = homepage + " <font color=\"#999999\" >下一页</font> "; } return homepage; }
由于作者没有实现最后一页的功能,不过代码看到这也有了一定的理解,所以要靠自己动手啦