Last Updated: February 25, 2016
·
2.326K
· jdp

Numbered Paging in SimpleDB Queries

Amazon SimpleDB doesn't support OFFSET clauses in its query syntax like some other storage systems do. For example, if you're using pages of 10 items each in your app, here is how you might get the 4th page in MySQL:

SELECT * FROM items LIMIT 30, 10

The LIMIT 30, 10 clause means skip the first 30 records and return the 10 after them. SimpleDB doesn't have this out of the box, but you can simulate it by doing 2 queries. The first is this one, which counts up the first 30 items:

SELECT COUNT(*) FROM items LIMIT 30

Your SimpleDB client library will execute this, returning the count and also a NextToken element in the response. Now you can make this second query to get the 4th page of items:

SELECT * FROM items LIMIT 10

Make sure that you pass along the NextToken into your client library so that the query is resumed from the associated cursor. Any query issued with that next token only operates on the records that weren't counted by the first query, giving you an implicit offset.