Index Question

35 views
Skip to first unread message

Joshua Smith

unread,
Feb 24, 2016, 9:52:01 AM2/24/16
to Google App Engine
I just wrote a new app and it has an entity that I use to show a leader board:

class CountModel(db.Model):
count = db.IntegerProperty(default=0)
trend = db.FloatProperty(default=0.0)

I get the list of these using a simple query:

if self.request.get("mode") == "trend":
counts = CountModel.gql("ORDER BY trend DESC").fetch(20)
else:
counts = CountModel.gql("ORDER BY count DESC").fetch(20)

I ran this in my dev environment and then checked index.yaml to make sure it made the indexes. It didn’t.

So I pushed it to production, fully expecting to see an error when I ran the query because I didn’t have this index defined. No error.

It works perfectly.

I also have this query in a cron job, and it works fine without the index, too:

for c in CountModel.gql("WHERE trend > 1"):

Has something changed? It used to be that to do an ordered query, or an inequality query, you had to have an index.

-Joshua

Alex Martelli

unread,
Feb 24, 2016, 10:03:30 AM2/24/16
to google-a...@googlegroups.com
You still do, but single-property indices are generated automatically. That's documented at https://cloud.google.com/appengine/docs/python/datastore/indexes : "App Engine predefines a simple index on each property of an entity. An App Engine application can define further custom indexes".

and later in the same page, to clarify:

The Datastore builds automatic indexes for queries of the following forms:

Kindless queries using only ancestor and key filters
Queries using only ancestor and equality filters
Queries using only inequality filters (which are limited to a single property) 

etc. Your use case falls within the third one of these.


Alex


-Joshua

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/38ED761B-EEBE-4CED-80B6-ADF42357BF57%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Joshua Smith

unread,
Feb 24, 2016, 10:55:32 AM2/24/16
to google-a...@googlegroups.com
Sweet! Thank you. I’ve been on GAE since the beginning, and I guess you added that at some point after I stopped reading the documentation ;)

Reply all
Reply to author
Forward
0 new messages