Elffuss BraidHub

kikocis/java-driver public

Explain offset queries in paging doc.
Changeset2e026b5b60b86cc51716651f6bccf66c6e7305d6a8ae74d70eef56e5eba9c592
Authorolim7t <omichallat+github@gmail.com> · 2015-06-03 16:53 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-03 17:45 +02:00
Parents2ce43425d440

Browse the files at this changeset

2 files changed

modified faq/README.md +0 −4
@@ -10,7 +10,3 @@
1010 Native protocol v1 does not support paging, but you can emulate it in
1111 CQL with `LIMIT` and the `token()` function. See
1212 [this conversation](https://groups.google.com/a/lists.datastax.com/d/msg/java-driver-user/U2KzAHruWO4/6vDmUVDDkOwJ) on the mailing list.
13-
14-There is no trivial solution for offset queries (e.g. jump to page 20
15-directly). Cassandra does not implement them out of the box, see
16-[CASSANDRA-6511](https://issues.apache.org/jira/browse/CASSANDRA-6511).
modified features/paging/README.md +47 −1
@@ -93,7 +93,7 @@
9393 }
9494 ```
9595
96-### Manual paging
96+### Saving and reusing the paging state
9797
9898 Sometimes it is convenient to save the paging state in order to restore
9999 it later. For example, consider a stateless web service that displays a
@@ -216,3 +216,49 @@
216216
217217 [gpsu]: http://www.datastax.com/drivers/java/2.1/com/datastax/driver/core/ExecutionInfo.html#getPagingStateUnsafe()
218218 [spsu]: http://www.datastax.com/drivers/java/2.1/com/datastax/driver/core/Statement.html#setPagingStateUnsafe(byte[])
219+
220+
221+### Offset queries
222+
223+Saving the paging state works well when you only let the user move from
224+one page to the next. But it doesn't allow random jumps (like "go
225+directly to page 10"), because you can't fetch a page unless you have
226+the paging state of the previous one. Such a feature would require
227+*offset queries*, but they are not natively supported by Cassandra (see
228+[CASSANDRA-6511](https://issues.apache.org/jira/browse/CASSANDRA-6511)).
229+The rationale is that offset queries are inherently inefficient (the
230+performance will always be linear in the number of rows skipped), so the
231+Cassandra team doesn't want to encourage their use.
232+
233+If you really want offset queries, you can emulate them client-side.
234+You'll still get linear performance, but maybe that's acceptable for
235+your use case. For example, if each page holds 10 rows and you show at
236+most 20 pages, this means you'll fetch at most 190 extra rows, which
237+doesn't sound like a big deal.
238+
239+For example, if the page size is 10, the fetch size is 50, and the user
240+asks for page 12 (rows 110 to 119):
241+
242+* execute the statement a first time (the result set contains rows 0 to
243+ 49, but you're not going to use them, only the paging state);
244+* execute the statement a second time with the paging state from the
245+ first query;
246+* execute the statement a third time with the paging state from the
247+ second query. The result set now contains rows 100 to 149;
248+* skip the first 10 rows of the iterator. Read the next 10 rows and
249+ discard the remaining ones.
250+
251+You'll want to experiment with the fetch size to find the best balance:
252+too small means many background queries; too big means bigger messages
253+and too many unneeded rows returned (we picked 50 above for the sake of
254+example, but it's probably too small -- the default is 5000).
255+
256+Again, offset queries are inefficient by nature. Emulating them
257+client-side is a compromise when you think you can get away with the
258+performance hit. We recommend that you:
259+
260+* test your code at scale with the expected query patterns, to make sure
261+ that your assumptions are correct;
262+* set a hard limit on the highest possible page number, to prevent
263+ malicious users from triggering queries that would skip a huge amount
264+ of rows.