Random Sort Order with Datastore

66 views
Skip to first unread message

Rob

unread,
Apr 12, 2008, 8:37:56 AM4/12/08
to Google App Engine
Is it possible to sort in a random order with datastore? I'm looking
for something like the MySQL query

SELECT * FROM table ORDER BY RAND();

Joscha Feth

unread,
Apr 12, 2008, 9:58:57 AM4/12/08
to Google App Engine
there is no such thing as a random function (or any user-defined
functions at all) at the moment.
But how about fetching your objects and randomizing the list
afterwards:

random.shuffle(yourlist)

regards,
Joscha

Bryan A. Pendleton

unread,
Apr 17, 2008, 1:16:12 PM4/17/08
to Google App Engine
If you don't need it to be random *each time*, but only a set of
random sequences, you can generate a random value (or 2 or 3) on
insert/update, and then use that ordering when you do a lookup. If
you'll have a huge number of items, having 2 or 3 of these and picking
a random starting point for each query will be a reasonably proxy for
"random order", since you can only get about 1000 at a time anyway.

Bryan A. Pendleton

unread,
Apr 17, 2008, 1:47:47 PM4/17/08
to Google App Engine
If you don't need it to be random *each time*, but only a set of
random sequences, you can generate a random value (or 2 or 3) on
insert/update, and then use that ordering when you do a lookup. If
you'll have a huge number of items, having 2 or 3 of these and picking
a random starting point for each query will be a reasonably proxy for
"random order", since you can only get about 1000 at a time anyway.

On Apr 12, 9:58 am, Joscha Feth <jos...@feth.com> wrote:
Reply all
Reply to author
Forward
0 new messages