I should clarify, I'm not saying move the 1000-entity fetch to the
write process, but instead keep a running total of sum and count that
you can increment and use to calculate the average, rather than having
to fetch entities.  This doesn't solve the use case of "average of
last x entities", though (I made the assumption that you'd prefer to
have the average of >1000 entities if possible) - for that you could
use a list property of length x as a queue and use the sum() and len()
functions to get the average.

On Jun 22, 4:46 pm, Tony <fatd...@gmail.com> wrote:
> You could accomplish this task like so:
>
> xlist = []
> query = Foo.all().filter("property_x >" 50).order("-timestamp")
> for q in query:
>   xlist.append(q.property_x)
> avg = sum(xlist) / len(xlist)
>
> What Nick is saying, I think, is that fetching 1000 entities is going
> to be very resource-intensive, so a better way to do it is to
> calculate this data at write-time instead of read-time.  For example,
> every time you add an entity, you could update a separate entity that
> has a property like "average = db.FloatProperty()" with the current
> average, and then you could simply fetch that entity and get the
> current running average.
>
> On Jun 22, 4:25 pm, herbie <4whi...@o2.co.uk> wrote:
>
> > Ok. Say I have many (>1000)  Model entities with two properties 'x'
> > and 'date'.    What is the most efficient query to fetch say the
> > latest 200 entities  where x > 50.   I don't care what their 'date's
> > are as long as I get the latest and x > 50
>
> > Thanks again for your help.
>
> > On Jun 22, 4:11 pm, "Nick Johnson (Google)" <nick.john...@google.com>
> > wrote:
>
> > > Consider precalculating this data and storing it against another entity.
> > > This will save a lot of work on requests.
>
> > > -Nick Johnson
>
> > > On Mon, Jun 22, 2009 at 3:55 PM, herbie <4whi...@o2.co.uk> wrote:
>
> > > > No the users won't need to read 1000 entities, but I want to calculate
> > > > the average of a  property from the latest 1000 entities.
>
> > > > On Jun 22, 3:30 pm, "Nick Johnson (Google)" <nick.john...@google.com>
> > > > wrote:
> > > > > Correct. Are you sure you need 1000 entities, though? Your users 
> > > > > probably
> > > > > won't read through all 1000.
>
> > > > > -Nick Johnson
>
> > > > > On Mon, Jun 22, 2009 at 3:23 PM, herbie <4whi...@o2.co.uk> wrote:
>
> > > > > > So to be sure to get the latest 1000 entities I should add a 
> > > > > > datetime
> > > > > > property to my entitie model and filter and sort on that?
>
> > > > > > On Jun 22, 1:42 pm, herbie <4whi...@o2.co.uk> wrote:
> > > > > > > I know that if there are more than 1000 entities that match a 
> > > > > > > query,
> > > > > > > then only 1000 will  be return by fetch().  But my question is 
> > > > > > > which
> > > > > > > 1000? The last 1000 added to the datastore?  The first 1000 added 
> > > > > > > to
> > > > > > > the datastore? Or is it undedined?
>
> > > > > > > Thanks
> > > > > > > Ian
>
> > > > > --
> > > > > Nick Johnson, App Engine Developer Programs Engineer
> > > > > Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration
> > > > Number:
> > > > > 368047
>
> > > --
> > > Nick Johnson, App Engine Developer Programs Engineer
> > > Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration Number:
> > > 368047
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to