[google-appengine] Re: Sorting more than 1000 entities

2009-05-19 Thread daiski

Thanks Zheng and Nick!

On May 19, 3:41 pm, "Nick Johnson (Google)" 
wrote:
> Hi Daiski,
>
> You can sort your results by using the .order() method on a Query object:
>
> q = Student.all().order('last_name').
>
> The datastore only supports fetching 1000 results per query. If you
> really need to retrieve more, you can use multiple queries and
> paginate, for example see Zheng's solution (but bear in mind that his
> code snippet will omit entries in the case of more than one student
> with the same last name).
>
> Before you do so, though, consider why you're trying to retrieve more
> than 1000 results in a single request. Do you intend to display them
> all to the user? If so, is that a usable user interface? Do you intend
> to aggregate the data from the records? In this case, you may want to
> consider calculating the aggregates ahead of time.
>
> -Nick Johnson
>
> On Mon, May 18, 2009 at 9:16 PM, daiski  wrote:
>
> > Hi.
>
> > I have more than 1000 entities in one kind (Students).
>
> > How can I retrieve from (or iterate through) the datastore *all*
> > Students sorted by a StringProperty last_name?
>
> > Thanks :)
>
> > Mo
> > --~--~-~--~~~---~--~~
> > You received this message because you are subscribed to the Google Groups 
> > "Google App Engine" group.
> > To post to this group, send email to google-appengine@googlegroups.com
> > To unsubscribe from this group, send email to 
> > google-appengine+unsubscr...@googlegroups.com
> > For more options, visit this group 
> > athttp://groups.google.com/group/google-appengine?hl=en
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: Sorting more than 1000 entities

2009-05-19 Thread Nick Johnson (Google)

Hi Daiski,

You can sort your results by using the .order() method on a Query object:

q = Student.all().order('last_name').

The datastore only supports fetching 1000 results per query. If you
really need to retrieve more, you can use multiple queries and
paginate, for example see Zheng's solution (but bear in mind that his
code snippet will omit entries in the case of more than one student
with the same last name).

Before you do so, though, consider why you're trying to retrieve more
than 1000 results in a single request. Do you intend to display them
all to the user? If so, is that a usable user interface? Do you intend
to aggregate the data from the records? In this case, you may want to
consider calculating the aggregates ahead of time.

-Nick Johnson

On Mon, May 18, 2009 at 9:16 PM, daiski  wrote:
>
> Hi.
>
> I have more than 1000 entities in one kind (Students).
>
> How can I retrieve from (or iterate through) the datastore *all*
> Students sorted by a StringProperty last_name?
>
> Thanks :)
>
> Mo
> --~--~-~--~~~---~--~~
> You received this message because you are subscribed to the Google Groups 
> "Google App Engine" group.
> To post to this group, send email to google-appengine@googlegroups.com
> To unsubscribe from this group, send email to 
> google-appengine+unsubscr...@googlegroups.com
> For more options, visit this group at 
> http://groups.google.com/group/google-appengine?hl=en

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: Sorting more than 1000 entities

2009-05-19 Thread 狼外婆

The problem is not about sorting. The problem is that you cannot get
more than 1000 entries.
You need to "paginate" on the last_name property (if it is guaranteed
to be unique).

all_students = [ ]
next = ''
while True:
students = Student.all().order('last_name').filter('last_name >',
next).fetch(100)
if students:
all_students.extends(students)
next = all_students[-1].last_name
else:
break

Just pay attention that the code above may use too much CPU time if
you really have a lot of entries.

More details about paging:

http://code.google.com/intl/en/appengine/articles/paging.html

Regards.

ZHENG Zhong
http://buggarden.zhengzhong.net/

On May 18, 10:16 pm, daiski  wrote:
> Hi.
>
> I have more than 1000 entities in one kind (Students).
>
> How can I retrieve from (or iterate through) the datastore *all*
> Students sorted by a StringProperty last_name?
>
> Thanks :)
>
> Mo
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---