Appengine数据存储,一个索引用于多个查询

2024-06-16 14:40:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个查询在代码中使用多个属性。例如,假设一个ndb.型号被叫用户

还有一些疑问:

user.query(user.enabled == Ture, user.name == "demo").order(user.age).fetch(limit=10)
user.query(user.enabled == True, user.domain == "demo.com").fetch(limit=10)
user.query(user.enabled == False, user.phone == 2).fetch(limit=10)

我只想使用一个索引(包含所有字段)来减少一个大的集合。(谷歌限制为200)

^{pr2}$

但不起作用。在

如果不可能,有没有办法提高指数上限?在

对不起,我的英语。在


我不知道我是否明白。在

这是一个真实的样品。在

class dbRealEstateImages(ndb.Model):
    realestate = ndb.IntegerProperty(required=True)
    md5 = ndb.StringProperty()
    image_file = ndb.BlobKeyProperty()
    image_url = ndb.StringProperty()
    position = ndb.IntegerProperty(default=0)

我想要这个问题:

dbRealEstateImages.query(dbRealEstateImages.realestate == reID).order(dbRealEstateImages.position).fetch(limit=None, projection=[dbRealEstateImages.image_url])

我们有3个属性,一个用于筛选,另一个用于排序,另一个用作投影。在

因此,我们需要一个基于文档的属性和排序顺序索引,可以是这样的:

Index(dbRealestateImage, realestate, position)
Index(dbRealestateImage, image_url, position)

但是,按升序排列,我们不需要包含在索引中,因此:

Index(dbRealestateImage, realestate)
Index(dbRealestateImage, image_url)

如果我们只有一个属性,我们不需要索引,所以我认为,“这个查询不需要索引”

但阿彭金提高了

no matching index found.
The suggested index for this query is:
- kind: dbRealEstateImages
  properties:
  - name: realestate
  - name: position
  - name: image_url

所以,我有点困惑:(

p.D.:在我的配额详细信息中,所有索引都是count、autogenerated、included。在


Tags: nameimageurlindex属性enabledpositionfetch
1条回答
网友
1楼 · 发布于 2024-06-16 14:40:36

不能只使用一个索引,它们必须真正针对查询才能快速。但是,对于大多数示例查询,自动索引可以正常工作,这要归功于zigzag merge join algorithm,而且这些索引不计入200个索引限制。在

排序将需要自定义索引,但仅针对每个属性,例如:

Index(User, domain, -age)
Index(User, enabled, -age)
Index(User, name, -age)
Index(User, phone, -age)

这4个索引应该使您能够使用任何按年龄排序的查询,而不管您按什么属性进行筛选(如果您要为需要16个的每个排列添加复合索引)。在

唯一需要注意的是,数据的形状将决定算法运行的效率;这是非常聪明的,但是您应该阅读文档的performance部分,以便知道如何处理复杂的情况。在

像往常一样,您应该首先度量;当您检测到公共查询的执行速度不如您需要的快时,那么您可以为该特定情况添加一个复合索引。在

我强烈建议您阅读Index Selection and Advanced Search文档,其中详细介绍了所有这些信息。在

至于提高200上限,如果您联系支持部门,您可以作为卓越理财客户来完成(从来没有这样做过,所以不能说它有多可行),但我强烈建议您首先优化索引的使用;拥有大量索引很容易,但也会非常昂贵,因此花时间思考最佳策略是值得的(我发现我其实并不需要那么多)。在

相关问题 更多 >