Use Twitter's cursor method to paging Web data bitsCN.com
This article discusses how to implement the data paging function in Web applications. different technical implementation methods differ in performance.
For the technical implementation method of the function, take MySQL as an example.
Select * from msgs where thread_id =? Limit page * count, count
However, when reading the Twitter API, we found that many interfaces use cursor instead of page and count, such as the followers ids interface.
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 =-1, 1300794057949944903
From the above description, we can see that the call to the http://twitter.com/followers/ids.xml needs to pass the cursor parameter for paging, rather than the traditional url? Page = n & count = n. What are the advantages of doing so? Do you want each cursor to maintain an image of the current dataset? Does the query result have duplicate content due to real-time changes in the result set?
Twitter architect John Kalucki mentioned in Google Groups's Cursor Expiration discussion
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
Reverse chron sorted list. So, since you can't change the past, other
Erasing it, it's got tively stable. (Modifications bubble to the top.)
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.
John mentioned in another new cursor-based pagination not multithread-friendly
The page based approach does not scale with large sets. We can no
Longer support this kind of API without throwing a painful number
503 s.
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
Page size of 5,000.
Also, scraping the social graph repeatedly at high speed is cocould
Often be considered a low-value, borderline abusive use of the social
Graph API.
Through these two paragraphs, we are very clear. for big result set data, the purpose of using cursor is to greatly improve performance. Take MySQL as an example. for example, if the number of pages reaches 100,000, you do not need to use cursor. the corresponding SQL is
Select * from msgs limit 100000,100
On a table with millions of records, it takes more than five seconds to execute the SQL statement for the first time.
Assume that we use the value of the table's primary key as the cursor_id, and the SQL statement corresponding to the cursor paging method can be optimized
Select * from msgs where id> cursor_id limit 100;
In the same table, it usually takes less than MS, and the efficiency will be improved by dozens of times. For performance differences between MySQL limit, see an immature article I wrote three years ago about MySQL LIMIT performance.
Conclusion
We recommend that you use this cursor method to flip a large dataset in a Web application. However, the disadvantage of this method is that pages must be consecutive and cannot be jumped.
BitsCN.com