Hierarchy Optimization on Google Appengine Datastore

后端 未结 2 473
遇见更好的自我
遇见更好的自我 2021-02-06 16:33

I have hierarchical data stored in the datastore using a model which looks like this:

class ToolCategories(db.Model):  
   name = db.StringProperty()  
   parent         


        
相关标签:
2条回答
  • 2021-02-06 17:06

    You have a very reasonable approach! My main caveat would be one having little to do with GAE and a lot with Python: don't build a string from pieces with + or +=. Rather, you make a list of string pieces (with append or extend or list comprehensions &c) and when you're all done you join it up for the final string result with ''.join(thelist) or the like. Even though recent Python versions strive hard to optimize the intrinsically O(N squared) performance of the + or += loops, in the end you're always better off building up lists of strings along the way and ''.joining them up at the very end!

    0 讨论(0)
  • 2021-02-06 17:08

    The main disadvantage of your approach is that because you're using the "adjacency list" way of representing trees, you have to do one datastore query for each branch of the tree. Datastore queries are fairly expensive (around 160ms each), so constructing the tree, particularly if it's large, could be rather expensive).

    There's another approach, which is essentially the one taken by the datastore for representing entity groups: Instead of just storing the parent key, store the entire list of ancestors using a ListProperty:

    class ToolCategories(db.Model):
      name = db.StringProperty()
      parents = db.ListProperty(db.Key)
    

    Then, to construct the tree, you can retrieve the entire thing in one single query:

    q = ToolCategories.all().filter('parents =', root_key)
    
    0 讨论(0)
提交回复
热议问题