app engine's back referencing is too slow. How can I make it faster?
Posted
by Ray Yun
on Stack Overflow
See other posts from Stack Overflow
or by Ray Yun
Published on 2010-05-12T09:09:22Z
Indexed on
2010/05/12
9:14 UTC
Read the original article
Hit count: 161
google-app-engine
|google-datastore
Google app engine has smart feature named back references and I usually iterate them where the traditional SQL's computed column need to be used.
Just imagine that need to accumulate specific force's total hp.
class Force(db.Model):
hp = db.IntegerProperty()
class UnitGroup(db.Model):
force = db.ReferenceProperty(reference_class=Force,collection_name="groups")
hp = db.IntegerProperty()
class Unit(db.Model):
group = db.ReferenceProperty(reference_class=UnitGroup,collection_name="units")
hp = db.IntegerProperty()
When I code like following, it was horribly slow (almost 3s) with 20 forces with single group - single unit. (I guess back-referencing force reload sub entities. Am I right?)
def get_hp(self):
hp = 0
for group in self.groups:
group_hp = 0
for unit in group.units:
group_hp += unit.hp
hp += group_hp
return hp
How can I optimize this code? Please consider that there are more properties should be computed for each force/unit-groups and I don't want to save these collective properties to each entities. :)
© Stack Overflow or respective owner