日期时间 属性 和育儿的 NDB 索引
NDB index of datetime property and parenting
我对自己对索引的理解不是很自信。
(舒适度:87%)
父子关系是否影响 BigTable 平板电脑上的索引位置。还是纯粹由种类决定?
我的例子:
通常情况下,如果实体类型经常写入,则索引日期时间 属性 不是一个好主意。
但是,如果父项是一个相当均匀分布的随机密钥,并且不太可能会有 2 个 Proposed() 实体具有相同的父项,我仍然会对 单调增加索引值创建热点?
(我使用的是 App Engine 标准,Python 2.7。)
‘’’
#...我有一个这样的实体:
class Proposed(ndb.Model):
foo = ndb.StringProperty(indexed=True, default=None)
bar = ndb.IntegerProperty(indexed=True, default=0)
date = ndb.DateTimeProperty(indexed=True, auto_now_add=True)
#… create a randomly distributed key
random_id = int(random.uniform(0, 9999999999999999))
parent_key = ndb.Key(‘Papa', random_id)
#…I parent the entity to the random key
p = Proposed(parent=parent_key)
p.foo = ‘a ball of string’
p.bar = 42
p.put()
#…and I query using inequality filter
q = Proposed.query(ndb.AND(Proposed.bar == 42,
Proposed.date >= start_date,
Proposed.date < end_date))
'''
似乎表明此(祖先)解决方案的文档:
https://cloud.google.com/appengine/articles/indexselection
描述索引层次结构。
https://cloud.google.com/appengine/docs/standard/python/datastore/indexes#index-definition-structure
"The rows of an index table are sorted first by ancestor and then by property values, in the order specified in the index definition."
https://cloud.google.com/datastore/docs/best-practices#high_readwrite_rates_to_a_narrow_key_range
“避免对字典顺序接近的 Cloud Datastore 密钥进行高读取或写入速率。
Cloud Datastore 建立在 Google 的 NoSQL 数据库 Bigtable 之上,并受制于 Bigtable 的性能特征。 Bigtable 通过将行分片到单独的 tablet 上来扩展,并且这些行按字典顺序按键排序
替代解决方案:
1) 创建一个日期时间字符串 属性,并在前面加上随机散列。
2) 使日期时间字符串的顺序相反:millisecond:second:minute:hour day:month:year
我知道这些解决方案如何使用等式过滤器进行查询,但是我将在日期上使用不等式过滤器,但我不知道如何使用该方法查询日期范围。
谦虚谢谢!
您将希望使用 (1) 的变体,其中您的前缀是众所周知的。如果您的前缀不是很清楚,您将很难通过时间戳查询您的实体。如果您不想按时间戳查询,则可以禁用时间戳属性的索引。 best practices guide 更多详情。
实体祖先对单调递增的索引值没有帮助,因为 Cloud Datastore 仍然允许您按种类查询,例如select * from Proposed order by date
。此查询不使用祖先,因此您可以得出所使用的索引不使用祖先。
我对自己对索引的理解不是很自信。
(舒适度:87%)
父子关系是否影响 BigTable 平板电脑上的索引位置。还是纯粹由种类决定?
我的例子:
通常情况下,如果实体类型经常写入,则索引日期时间 属性 不是一个好主意。
但是,如果父项是一个相当均匀分布的随机密钥,并且不太可能会有 2 个 Proposed() 实体具有相同的父项,我仍然会对 单调增加索引值创建热点?
(我使用的是 App Engine 标准,Python 2.7。)
‘’’ #...我有一个这样的实体:
class Proposed(ndb.Model):
foo = ndb.StringProperty(indexed=True, default=None)
bar = ndb.IntegerProperty(indexed=True, default=0)
date = ndb.DateTimeProperty(indexed=True, auto_now_add=True)
#… create a randomly distributed key
random_id = int(random.uniform(0, 9999999999999999))
parent_key = ndb.Key(‘Papa', random_id)
#…I parent the entity to the random key
p = Proposed(parent=parent_key)
p.foo = ‘a ball of string’
p.bar = 42
p.put()
#…and I query using inequality filter
q = Proposed.query(ndb.AND(Proposed.bar == 42,
Proposed.date >= start_date,
Proposed.date < end_date))
'''
似乎表明此(祖先)解决方案的文档:
https://cloud.google.com/appengine/articles/indexselection
描述索引层次结构。
https://cloud.google.com/appengine/docs/standard/python/datastore/indexes#index-definition-structure
"The rows of an index table are sorted first by ancestor and then by property values, in the order specified in the index definition."
https://cloud.google.com/datastore/docs/best-practices#high_readwrite_rates_to_a_narrow_key_range
“避免对字典顺序接近的 Cloud Datastore 密钥进行高读取或写入速率。 Cloud Datastore 建立在 Google 的 NoSQL 数据库 Bigtable 之上,并受制于 Bigtable 的性能特征。 Bigtable 通过将行分片到单独的 tablet 上来扩展,并且这些行按字典顺序按键排序
替代解决方案:
1) 创建一个日期时间字符串 属性,并在前面加上随机散列。
2) 使日期时间字符串的顺序相反:millisecond:second:minute:hour day:month:year
我知道这些解决方案如何使用等式过滤器进行查询,但是我将在日期上使用不等式过滤器,但我不知道如何使用该方法查询日期范围。
谦虚谢谢!
您将希望使用 (1) 的变体,其中您的前缀是众所周知的。如果您的前缀不是很清楚,您将很难通过时间戳查询您的实体。如果您不想按时间戳查询,则可以禁用时间戳属性的索引。 best practices guide 更多详情。
实体祖先对单调递增的索引值没有帮助,因为 Cloud Datastore 仍然允许您按种类查询,例如select * from Proposed order by date
。此查询不使用祖先,因此您可以得出所使用的索引不使用祖先。