Reputation: 297
I have a model for user to store their bookmarks. What is the best way to get the total number of bookmarks, should I create a field to store the number of total or aggregrate it when user retrieve the data ?
class UserBookMarks(Model):
class Meta:
behaviors = (searchable.Searchable,)
search_exclude=('created')
user= ndb.KeyProperty(kind=User, repeated=False)
items = ndb.KeyProperty(kind=ItemList, repeated=True)
created = ndb.DateTimeProperty(auto_now_add=True)
Upvotes: 0
Views: 364
Reputation: 4178
Counting or aggregating in the Datastore is inefficient. Therefore create a field to store the total. If a counter needs to be updated more than a few times per second (unlikely with user bookmarks) you would need to resort to sharded counters.
Upvotes: 2