【发布时间】:2011-02-27 22:22:24
【问题描述】:
谁能用简单的英语解释数据存储区中的 5000 索引上限。
这是否意味着存储对象的索引列表属性不能超过 5000 个元素?
【问题讨论】:
标签: google-app-engine google-cloud-datastore
谁能用简单的英语解释数据存储区中的 5000 索引上限。
这是否意味着存储对象的索引列表属性不能超过 5000 个元素?
【问题讨论】:
标签: google-app-engine google-cloud-datastore
Datastore 限制单个实体可以拥有的索引条目数,此限制设置为每个实体 5000 个元素。
您可以使用带有以下 sn-p 的Interactive shell 轻松测试此限制:
class Model(db.Model):
x = db.ListProperty(int)
entity = Model(x = range(5001))
entity.put()
'Too many indexed properties for entity %r.' % self.key())
BadRequestError: Too many indexed properties for entity
datastore_types.Key.from_path(u'Model', 0, _app=u'shell')
【讨论】:
Too many indexed 异常。尝试使用 5000 个元素应该可以代替。
简短的回答,是的,如果您对该属性进行了索引。
App Engine 会限制单个实体在索引上可以拥有的属性值的数量(行数 * 列数),因为它需要为每个排列创建一个索引。对于单个索引属性,您有 5000rows * 1column = 5000。
为了说明 App Engine 为何这样做,让我们以他们的 documentation 为例。
型号:
class MyModel(db.Model):
x = db.StringListProperty()
y = db.StringListProperty()
索引.yaml
indexes:
- kind: MyModel
properties:
- name: x
- name: y
执行
e2 = MyModel()
e2.x = ['red', 'blue']
e2.y = [1, 2]
e2.put()
在这种情况下,App Engine 将不得不为此数据存储条目单独创建 12 个索引,因为您可以有效地搜索任何值组合:
x1
x2
y1
y2
x1 y1
x1 y2
x2 y1
x2 y2
y1 x1
y1 x2
y2 x1
y2 x2
现在,如果您在每个属性中有 100 个值,您可以想象该列表会飙升至数量惊人的查询。
方程式是这样的:
len(x) + len(y) + (len(x)-1 * len(y) * (len(x) + len(y))) = number of indexed
**2 values per property**
2 + 2 + (1 * 2 * (2 + 2)) = 12
**100 values per property**
100 + 100 + (99 * 100 * (100 + 100)) = 1,980,200
【讨论】: