Which one of these GAE Datastore queries is more efficient?
Suppose I have defined my data models the follo开发者_Python百科wing way:
class Newsportal(db.Model):
name = db.StringProperty()
#... blah blah blah
class Article(db.Model):
newsportal = db.ReferenceProperty(Newsportal)
url = db.StringProperty()
date = db.DateTimeProperty()
#... blah blah blah
Now I want to determine whether there is an article with certain url in db (each url is unique). Which one of these queries should be more efficient:
Article.all().filter('newspaper = ', someNewspaper.key()).filter('date = ', someDate).filter('url = ', someUrl).get()
or
Article.all().filter('url = ', someUrl).get()
Assuming you have a custom index for the first query, they should have the same efficiency, although the datastore won't enforce uniqueness for you unless you use the URL as a key_name, in which case you don't need to query at all, and can use the much more efficient get_by_key_name.
I would think that both queries would execute in about the same amount of time. Both queries will need an index, and since the same number of entries will be in each index, I don't see what would make one take longer than the other.
You should look at your OTHER queries and see if you can arrange it so they can all use the same index.
精彩评论