mmierins
mmierins

Reputation: 3774

Which one of these GAE Datastore queries is more efficient?

Suppose I have defined my data models the following 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()

Upvotes: 0

Views: 110

Answers (2)

Riley Lark
Riley Lark

Reputation: 20890

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.

Upvotes: 0

Wooble
Wooble

Reputation: 90037

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.

Upvotes: 2

Related Questions