Google 数据存储索引优化

Google Datastore Index Optimization

在设计仅键查询以过滤 Google 数据存储区实体时,我生成了许多复合索引,它们是另一个索引的子集。是否可以对过滤已索引属性子集的查询使用相同的复合索引?例如,如果我有以下 key-only 查询,是否可以少于三个索引?

Query 1: Entities where a = 1, b = 1, c = 1;
Query 2: Entities where a = 1, b = 1;
Query 3: Entities where a = 1;

这是我正在处理的实际查询的示例:

Query<Key> query = Query.newKeyQueryBuilder()
              .setKind("track")
              .setFilter(CompositeFilter.and(PropertyFilter.eq("status", 1), PropertyFilter.eq("bpm", 138), PropertyFilter.eq("artist", "AVB"), PropertyFilter.eq("label", "Armada")))
              .setOrderBy(OrderBy.asc("date"))
              .build();

Datastore 可以将较小的索引合并在一起以支持较大的相等查询,请参阅 index merging。使用此功能,您的查询集的最小索引集类似于:

index.yaml

indexes:

- kind: Albums
  properties:
  - name: artist
  - name: date

- kind: Albums
  properties:
  - name: bpm
  - name: date

- kind: Albums
  properties:
  - name: label
  - name: date

- kind: Albums
  properties:
  - name: status
  - name: date

这支持对任意数量的这些属性进行相等查询,按日期排序。但是请注意,index merging has a performance trade-off in some cases.