Yarin
Yarin

Reputation: 183499

Cost of PolyModel over Model in Google App Engine

I was wondering what the costs are of using PolyModels over Models in Google App Engine, with respect to indexing, etc. Is there a base cost to using a PolyModel over a Model even if it's not ever subclassed?

Upvotes: 2

Views: 385

Answers (1)

Bryce Cutt
Bryce Cutt

Reputation: 1525

PolyModel uses a "class" list property to store all the classes that the PolyModel instance "is". So if you have a PolyModel Foo with a subclass called Bar any instance of Bar will have [u'Foo', u'Bar'] in it's "class" property. The instances of Bar are actually stored in the datastore as instances of Foo with the Bar model name in the "class" property.

The "class" property needs to be indexed so every write to a PolyModel subclass incurs an extra two writes to the "class" index per class name that is in the list (one for descending and one for ascending) so if the "class" property for a model contains [u'Foo', u'Bar'] it will take 4 extra index writes to write that model.

When you query the datastore for instances of Foo the datastore can look at every model that has Foo as a parent model without having to necessarily filter on the "class" property (I don't know for sure whether it does filter on class but I suspect it does not).

When you query the datastore for instances of Bar it actually queries for instances of Foo but applies a filter on the "class" property to filter the result down to models that are of type Bar. If you are not applying other filters and sort orders you would probably not notice this in your composite indexes but if you apply filters and sorts the datastore may require your composite indexes to contain the "class" property which may require you to have more composite indexes and to write to more composite indexes.

One of my applications uses multiple levels of inheritance on a PolyModel and almost every query sorts on a "created" DateTimeProperty. Every one of these queries requires a composite index with "class" in it. In my case the extra index writes have been completely worth it as the PolyModel allowed me to model the data in a nice way.

Updated Feb 7, 2012 to include information from @Nick.

Upvotes: 6

Related Questions