steve
steve

Reputation: 167

When do I need index for Google App Engine Datastore?

I am using Google App enginge Python 2.7 Standard environment with old Datastore (Not NDB) for my project.

I encountered a strange behavior of Datastore custom index.

I have model like this.

class EmailHistory(db.Model):
    code = db.StringProperty(required=True)
    account = db.ReferenceProperty(Account)
    group = db.ReferenceProperty(Group)
    ......

And I tried to fetch entities using query below.

q = EmailHistory.all().filter('account =', account).filter('code =', code).filter('group =', group)
lst = [e for e in q]

I was expecting I will need custom index for this query since I am querying by three properties. (My understanding is AppEngine create build-in index automatically for each property so I won't need to define index for query of single property. But if I want to query by multiple properties, I will need to define custom index)

But when I run it on local, it didn't autogenerate custom index. Moreover, when I run it on Google App engine server, I was able to fetch entities without having NeedIndexError.

My question is when Datatstore need custom index exactly? I have read through their official documentation but I didn't figure it out why I didn't need custom index for the query above. https://cloud.google.com/appengine/docs/standard/python/datastore/indexes

Upvotes: 0

Views: 134

Answers (1)

Jim Morrison
Jim Morrison

Reputation: 2887

Cloud Datastore is able to do queries where a merge join can be used. You can find more details at https://cloud.google.com/appengine/articles/indexselection .

On https://cloud.google.com/appengine/docs/standard/python/datastore/indexes#index-configuration, it says "These predefined indexes are sufficient to perform many simple queries, such as equality-only queries and simple inequality queries". Your query is an equality-only query.

Upvotes: 1

Related Questions