热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

用Twitter的cursor方式进行Web数据分页
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.

  在另外一篇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 <10^7 and

  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
推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • MybatisPlus入门系列(13) MybatisPlus之自定义ID生成器
    数据库ID生成策略在数据库表设计时,主键ID是必不可少的字段,如何优雅的设计数据库ID,适应当前业务场景,需要根据需求选取 ... [详细]
  • 这也太简单了!轻松操作Feign 服务调用使用 Zipkin 链路追踪!
    0、介绍分布式微服务时代,方便了业务的快速增长和服务的稳定,但是系统出现问题后,面对同业务多服务排查起来令人头大。这时候领导就想着集成分布式追踪系统。Zipkin是T ... [详细]
  • 个体都会学习的JavaScript之DOM树
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了JavaScript之DOM树相关的知识,希望对你有一定的参考价值。目录 ... [详细]
  • 推荐一个ASP的内容管理框架(ASP Nuke)的优势和适用场景
    本文推荐了一个ASP的内容管理框架ASP Nuke,并介绍了其主要功能和特点。ASP Nuke支持文章新闻管理、投票、论坛等主要内容,并可以自定义模块。最新版本为0.8,虽然目前仍处于Alpha状态,但作者表示会继续更新完善。文章还分析了使用ASP的原因,包括ASP相对较小、易于部署和较简单等优势,适用于建立门户、网站的组织和小公司等场景。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • AstridDAO 专访:波卡稳定币黑马 BAI
    加入Pol ... [详细]
  • OAuth2.0指南
    引言OAuth2.0是一种应用之间彼此访问数据的开源授权协议。比如,一个游戏应用可以访问Facebook的用户数据,或者一个基于地理的应用可以访问Foursquare的用户数据等。 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了Flutter添加APP启动StoryView相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 范式转移:构建超级应用——胖应用 + 胖协议
    范式转移:构建超级应用——胖应用 + 胖协议 ... [详细]
  • 最全最实用的iPhone使用快捷键集合
      搜集整理的iPhone快捷键操作,虽然表面上iPhone按键只有一个HOME键,大部分操作都依赖触摸屏苹果设置快捷键。但是挡不住各种组合:Power键+Ho ... [详细]
  • Noticedmycamlistoreinstancewasdownaftermylatestupdate. ... [详细]
  • 据官方统计,截止至本周一,仅纽约市的Tumblr博文就已超过200亿条,对比六个月之前,这个数量整整多了一倍。而Tumblr ... [详细]
author-avatar
oyy8610406
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有