0

0

用Twitter的cursor方式进行Web数据分页_MySQL

php中文网

php中文网

发布时间:2016-06-01 13:49:04

|

1207人浏览过

|

来源于php中文网

原创

bitsCN.com

  本文讨论web应用中实现数据分页功能,不同的技术实现方式的性能方区别。

/

  上图功能的技术实现方法拿MySQL来举例就是

  select * from msgs where thread_id = ? limit page * count, count

  不过在看Twitter API的时候,我们却发现不少接口使用cursor的方法,而不用page, count这样直观的形式,如 followers ids 接口

 

  URL:

  http://twitter.com/followers/ids.format

  Returns an array of numeric IDs for every user following the specified user.

  Parameters:

  * cursor. Required. Breaks the results into pages. Provide a value of -1 to begin paging. Provide values as returned to in the response body’s next_cursor and previous_cursor attributes to page back and forth in the list.

  o Example: http://twitter.com/followers/ids/barackobama.xml?cursor=-1

  o Example: http://twitter.com/followers/ids/barackobama.xml?cursor=-1300794057949944903

 

  从上面描述可以看到,http://twitter.com/followers/ids.xml 这个调用需要传cursor参数来进行分页,而不是传统的 url?page=n&count=n的形式。这样做有什么优点呢?是否让每个cursor保持一个当时数据集的镜像?防止由于结果集实时改变而产生查询结果有重复内容?

  在Google Groups这篇Cursor Expiration讨论中Twitter的架构师John Kalucki提到

 

  A cursor is an opaque deletion-tolerant index into a Btree keyed by source

  userid and modification time. It brings you to a point in time in the

  reverse chron sorted list. So, since you can’t change the past, other than

  erasing it, it’s effectively stable. (Modifications bubble to the top.) But

  you have to deal with additions at the list head and also block shrinkage

  due to deletions, so your blocks begin to overlap quite a bit as the data

  ages. (If you cache cursors and read much later, you’ll see the first few

  rows of cursor[n+1]’s block as duplicates of the last rows of cursor[n]’s

  block. The intersection cardinality is equal to the number of deletions in

  cursor[n]’s block). Still, there may be value in caching these cursors and

  then heuristically rebalancing them when the overlap proportion crosses some

  threshold.

手机在线人工冲值
手机在线人工冲值

说明:我不知道这个系统还能用到什么地方!他的运作方式是这样的,客户在其他地方比如掏宝购买了 你得卡,然后在你的网站进行冲值,你得有人登陆并看着后台,如果有人冲值,就会刷出记录,手工冲值完毕后,你得点击 [冲值完毕],客户的页面 就会返回 冲值信息!安装:上传所有文件,倒入(sql.txt)mysql数据库,使用myphpadminphplib 777phplib/sys.php 777phplib

下载

 

  在另外一篇new cursor-based pagination not multithread-friendly中John又提到

 

  The page based approach does not scale with large sets. We can no

  longer support this kind of API without throwing a painful number of

  503s.

  Working with row-counts forces the data store to recount rows in an O

  (n^2) manner. Cursors avoid this issue by allowing practically

  constant time access to the next block. The cost becomes O(n/

  block_size) which, yes, is O(n), but a graceful one given n

  a block_size of 5000. The cursor approach provides a more complete and

  consistent result set.

  Proportionally, very few users require multiple page fetches with a

  page size of 5,000.

  Also, scraping the social graph repeatedly at high speed is could

  often be considered a low-value, borderline abusive use of the social

  graph API.

 

  通过这两段文字我们已经很清楚了,对于大结果集的数据,使用cursor方式的目的主要是为了极大地提高性能。还是拿MySQL为例说明,比如翻页到100,000条时,不用cursor,对应的SQL为

  select * from msgs limit 100000, 100

  在一个百万记录的表上,第一次执行这条SQL需要5秒以上。

  假定我们使用表的主键的值作为cursor_id, 使用cursor分页方式对应的SQL可以优化为

  select * from msgs where id > cursor_id limit 100;

  同样的表中,通常只需要100ms以下, 效率会提高几十倍。MySQL limit性能差别也可参看我3年前写的一篇不成熟的文章 MySQL LIMIT 的性能问题。

  结论

  建议Web应用中大数据集翻页可以采用这种cursor方式,不过此方法缺点是翻页时必须连续,不能跳页。

bitsCN.com

相关专题

更多
高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

43

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

84

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

24

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

16

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

56

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

16

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

9

2026.01.15

ppt一键生成相关合集
ppt一键生成相关合集

本专题整合了ppt一键生成相关教程汇总,阅读专题下面的的文章了解更多详细内容。

26

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Node.js 教程
Node.js 教程

共57课时 | 8.8万人学习

Rust 教程
Rust 教程

共28课时 | 4.5万人学习

Vue 教程
Vue 教程

共42课时 | 6.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号