Take the 2-minute tour ×

Currently, to determine whether or not there is a next page of entities I'm using the following code:

q = Entity.all().fetch(10)
cursor = q.cursor()
extra = q.fetch(1)

has_next_page = False
if extra:
  has_next_page = True

However, this is very expensive in terms of the time it takes to execute the 'extra' query. I need to extract the cursor after 10 results, but I need to fetch 11 to see if there is a succeeding page.

Anyone have any better methods?

share|improve this question
Does the datastore API cache results, I can't remember? If so, maybe fetch 11, check the count, then fetch 10 from a new query to get the cursor. Provided the results of the first fetch are cached, the second fetch would then be cheap. –  Steve Jessop Aug 30 '10 at 0:33
I don't think it does. I'll check. –  Matthew H Aug 30 '10 at 0:49
Nah, I don't think it does either. I guess you could fall back to the non-cursor tricks: record the value from the 11th entity of whatever you're sorting by, then do a >= filter on that for the next page. Esp. if it's impossible for multiple entities to have equal values of that field. –  Steve Jessop Aug 30 '10 at 2:24
add comment

1 Answer

up vote 1 down vote accepted

If you fetch 11 items straight away you'll only have to fetch 1 extra item to know if there is a next page or not. And you can just display the first 10 results and use the 11th result only as a "next page" indicator.

share|improve this answer
That's what I thought at first, but querying for 11 up front doesn't yield a cursor positioned after the 10th item, which the questioner wants. –  Steve Jessop Aug 30 '10 at 0:47
But then my cursor (which I use as a starting point for the next page) will be invalid (it will start the next query at the 12th item). This will lead to one entity missing per page. –  Matthew H Aug 30 '10 at 0:49
@Matt, just put the "extra item" in memcache at a suitably unique key, and on "next page" just check if there's something at that unique key (and if so delete it and prepend it to the set fetched from the cursor). –  Alex Martelli Aug 30 '10 at 2:59
@Matt H: that is indeed a downside, but easily worked around. As Alex Martelli already explained. –  Wolph Aug 30 '10 at 3:31
add comment

Your Answer


