Google App Engine中PolyModel over Model的成本 [英] Cost of PolyModel over Model in Google App Engine

查看:98
本文介绍了Google App Engine中PolyModel over Model的成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用PolyModel比Google App Engine中的模型在编制索引等方面的成本是多少。即使它没有被分类,在模型上使用PolyModel是否有基本成本?

解决方案

PolyModel使用class列表属性来存储PolyModel实例所在的所有类。所以如果你有一个名为Bar的子类的PolyModel Foo,Bar的任何实例都会在它的class属性中有[u'Foo',u'Bar']。 Bar的实例实际上作为Foo的实例存储在数据存储中,并在class属性中添加Bar模型名称。



需要将class属性编入索引所以每次写入PolyModel子类时都会对列表中的每个类名引发额外的两次写入(一个用于降序,一个用于升序),因此如果模型的class属性包含[u'Foo ',u'Bar']它将需要4个额外的索引写入来写入该模型。



当您为数据存储查询Foo实例时,数据存储可以查看每个模型它将Foo作为父模型,而不必过滤class属性(我不确定它是否对类进行过滤,但我怀疑它没有)。



在为Bar实例查询数据存储时,它实际上会查询Foo的实例,但会在class属性上应用过滤器以将结果过滤为Bar类型的模型。如果您没有应用其他筛选器和排序顺序,您可能不会在组合索引中注意到这一点,但是如果应用筛选器并对数据存储进行排序,则可能需要您的组合索引包含class属性,这可能需要您具有更多的组合索引并写入更多的复合索引。

其中一个应用程序在PolyModel上使用多级继承,几乎每个查询都在创建的DateTimeProperty上排序。每个查询都需要一个包含class的组合索引。在我的情况下,额外的索引写入已经完全值得,因为PolyModel允许我以很好的方式对数据建模。

2012年2月7日更新包含来自@Nick的信息。


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?

解决方案

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.

这篇关于Google App Engine中PolyModel over Model的成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆