Christina C.
Christina C.

Reputation: 31

google app engine paging and cursor

I'm trying to implement paging in my app on google app engine, using the python datastore api. My requirement is to display user data using infinite list. My current implementation is using cursor with something like this:

    //retrieve current_cursor
    all_data = my_data.all().order('-created')
    data = all_data.fetch(limit=10, start_cursor=current_cursor)
    current_cursor = all_data.cursor()
    //save current_cursor

From my understanding, cursor works on queried result_set, which saves the time of the subsequent queries (but not the first one), am I correct?

My concern is that if we have large amount of data, the first time query will still be too slow. To make the first time query faster, my thought is that, instead of querying all data, I'll break the query into multiple smaller ones, such as setting a filter with ('created >', certain_date) and using cursor to fetch results within each set.

However, the drawback is I'll have to maintain the certain_date on my own and if not choosing it wisely, it might cost me with lots of database read.

So my question is, do I understand the cursor correctly? Also, are there better ways to use google datastore to support this or any suggestions in general to implement this? Thanks!

Upvotes: 2

Views: 772

Answers (1)

Omair Shamshir
Omair Shamshir

Reputation: 2136

No, the first time query will not be slower. The advantage of using cursor is that it only processes the specific set, depending on cursor position and the limit, unlike to the Offset and limit combination which processes the whole data set and just show you the required portion.

Query cursors allow an application to retrieve a query's results in convenient batches without incurring the overhead of a query offset.

Regarding suggestions, for pagination, cursor is the best option, other suggestions are to use Projections, try to make best use of memcache by using get_by_id.

Upvotes: 2

Related Questions