kolistivra
kolistivra

Reputation: 4429

Performance difference between querying with key_name and a property

Assume I have the following entity kind:

class ExampleModel(db.Model):
    name = db.StringProperty()

mdl = ExampleModel(key_name='blah', name='abcd')
mdl.put()

Is there any performance difference between

ExampleModel.get_by_key_name('blah')

and

ExampleModel.all().filter('name = ', 'abcd').get()

If there's a performance difference, how much is it (roughly)? How does ExampleModel having an index for name influence the result?

Upvotes: 3

Views: 74

Answers (1)

proppy
proppy

Reputation: 10504

Yes, there is a performance (and price) difference.

In the query case you need to do 2 read operations:

  • 1 lookup in the index table
  • 1 lookup in the entity table

In the get_by_key case you only do 1 read operation:

  • 1 lookup for the entity table

Also note that indexed property also affect write performance and cost, as you need to do 1 additional write in the index table for reach indexed propery.

Feel free to take a look at this presentation about Datastore performance patterns and anti-patterns. By clicking the Run button on each side you can see the interactive appstats traces for each code snippets.

Upvotes: 6

Related Questions