Lets say you have an Entity like this.
postid=db.StringProperty()
comment=db.StringProperty()
for storing comments on a certain post identified by post id. The comments can hit billions of records. Now if you want to get all comments belonging to a certain post you can do,
query=Comment.all()
query.filter('postid = ','id').
Or instead of doing that you can define post like
class Post(db.Model)
commentids=db.StringListProperty()#store list of comment ids
This way you can directly get the comment by doing
comment=Comment.get_by_key_name('commentkey')
In the long run (when comments hit millions or even billions mark) which one is more efficient. In other words which one is more appropriate.
If you're planning to have billions of comments consider also using the newest NDB API, which, among other things, supports automatic caching.
Instead of filtering them by postid you should probably use a parent for your Comment entity. Here is an example (using DB, but it's very similar using NDB):
If you have model like this one:
class Post(db.Model):
desc = db.StringProperty()
class Comment(db.Model):
desc = db.TextProperty()
You can create posts and comments like:
post_db = Post(desc='Hello World')
post_db.put()
comment_db = Comment(parent=post_db, desc='Nice post')
comment_db.put()
And finally if you want to get all the comments from a particular post_db entity:
comment_dbs = Comment.all().ancestor(post_db)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With