According to Google engineer, fetch entry by key is very fast. So you should use fetch by key rather than manipulating the list. I think the B approach will cost more CPU also.
On Wed, Feb 10, 2010 at 1:38 AM, johnP <j...@thinkwave.com> wrote: > I'm trying to get my head around where to use the datastore for > business logic, and where to use python code. So just wanted to ask > two "which approach is better" questions: > > Scenario 1. For example, let's say you have a list of items, and you > need to return both the selected item as well as the list of items. > For example - a person has a bunch of cars: [Honda, Ford, Porsche, > Yugo, Trabaunt, Dusenberg]. You have stored the Yugo key as the > person's 'active_car'. > > You need to return: car, cars = Yugo, [Honda, Ford, > Porsche, Yugo, Trabaunt, Dusenberg] > > Which approach is better to return the values... > > Approach A: cars=person.car_set.fetch(1000) > car = db.get(stored_car_key) > return car, cars > > > Approach B: cars = person.car_set.fetch(1000) > car = [i for i in cars if i.key() == > stored_car_key] > return car, cars > > In other words - what's cheaper - the list comprehension, or the > db.get(). If the person has 1000 cars, does the answer change? > > > Scenario 2. I have a list of 300 people (In my case, there will never > be more than 1000) that I need to slice and dice in different ways. > a. Need them all, by last name, from California. b. Need people > between the ages of 25 and 35 in California. c. Need people over 300 > lbs in California. Which approach should I use: > > Approach A: Create multiple queries: > a. people = > state.people_set.order('last_name').fetch(1000) > b. people = > state.people_set.order('age').filter('age >', 35).etc. > c. people = > state.people_set.order(weight').filter('weight >', 300).etc. > > Approach B: Memcache the entire list of people, and > list_comprehension them into submission. For example: > > def return_people_by_last_name(): > people = get_all_people_from_memcache() > sort_list = [(i.last_name, i) for i in people > if i.state == state] > sort_list.sort() > return [i[1] for i in sort_list] > > def sort_people_by_weight(): > similar to above... > > In approach A, you'll be bearing the cost of additional indexes, as > well as bearing the cost that most of your returns will be hits to the > database. In approach B, you might be pulling 300 People from > memcache in order to return a single 300 pounder. > > Answers to these two questions might give me a better sense of when to > hit the datastore for business logic, and when to process using python > code. > > Thanks! > > johnP > > > > > > > -- > 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-appeng...@googlegroups.com. > To unsubscribe from this group, send email to > google-appengine+unsubscr...@googlegroups.com<google-appengine%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/google-appengine?hl=en. > > -- Stay hungry,Stay foolish. -- 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-appeng...@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.