首页 > 代码库 > 【思路】-分页-双top分页算法的原理
【思路】-分页-双top分页算法的原理
描述:实现分页的一种算法
大致过程:访客访问不同的分页,为这个当前页生成动态的查询SQL,然后送到数据库中执行
输入:总条数,每页多少条,第几页,查询的SQL,排序的字段
注意:传入的排序字段需要构成唯一记录,这就意味着不能单独的使用SortOrder这个单独排序
字段反转:SortOrder asc,PostID desc;反转之后是SortOrder desc,PostID asc;
实现过程
访问第一页
访问最后一页
访问前半数分页
访问后半数分页
100条数据 每页显示4条 按照ID降序 排序
当取第一页时:直接对所有数据降序取出前4条
当取第二页时:
1.先按降序获得前8条数据,然后升序排列着8条数据,
2.这样第二页的数据直接跑到了前面,取出4条,
3.然后在倒叙回去就是第二页的数据
同样的以此往下类推
当后半数分页时:
当取最后一页时:将所有数据升序过来,取出前4条即可
观察生成的过程
访问前半数分页
2016-06-17 08:48:34 SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:48:43 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 8 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:44 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 12 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:45 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 16 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:45 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 20 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:46 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 24 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:46 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 28 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:47 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 32 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:47 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 36 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:48 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 40 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
访问后半数分页
2016-06-17 08:50:51 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:51:02 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 5 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:03 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 9 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:03 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 13 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:04 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 17 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:06 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 21 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:07 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 25 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:07 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 29 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:08 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 33 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:09 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 37 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:09 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 41 ) AS T ORDER BY PostID desc LIMIT 4
来自为知笔记(Wiz)
【思路】-分页-双top分页算法的原理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。