数据存储区查询不起作用,要求索引多于预期

时间:2015-10-13 13:57:05

标签: indexing google-cloud-datastore zigzag

<datastore-index kind="Environment" ancestor="false">
    <property name="active" direction="asc" />
    <property name="consumed" direction="asc" />
</datastore-index>

<datastore-index kind="Environment" ancestor="false">
    <property name="active" direction="asc" />
    <property name="creationDate" direction="desc" />
</datastore-index>

我有以上两个索引

当我查询如下时,它不起作用,并且它表示需要新的索引。

SELECT * FROM Environment其中active = false且consume = true且creationDate&lt; “2013年9月22日

GQL响应如下:

找不到匹配的索引。 此查询的建议索引是:

<datastore-index kind="Environment" ancestor="false">
   <property name="active" direction="asc" />
   <property name="consumed" direction="asc" />
   <property name="creationDate" direction="asc" />
</datastore-index>

我做错了什么?它应该基于zigzag合并吗?

1 个答案:

答案 0 :(得分:1)

在查看zigzag查询所需的索引时,将索引拆分为前缀和后缀非常有用。索引的前缀用于回答等式,而后缀用于回答不等式和排序。

为了执行合并连接,查询中涉及的所有索引的后缀必须匹配,以便排序顺序相同。因此,对于您的查询:

SELECT * FROM Environment where active = false and consumed = true and creationDate < '2013-09-22'

creationDate必须在后缀中; activeconsumed必须位于前缀中。

索引:

<datastore-index kind="Environment" ancestor="false">
   <property name="active" direction="asc" />
   <property name="consumed" direction="asc" />
   <property name="creationDate" direction="asc" />
</datastore-index>
如果我们将索引拆分为consumedcreationDate之间的前缀和后缀,

可以满足此要求。但是,您也可以使用两个单独的索引来满足此要求:

<datastore-index kind="Environment" ancestor="false">
   <property name="active" direction="asc" />
   <property name="creationDate" direction="asc" />
</datastore-index>

<datastore-index kind="Environment" ancestor="false">
   <property name="consumed" direction="asc" />
   <property name="creationDate" direction="asc" />
</datastore-index>

在这种情况下,后缀将包含creationDate,前缀分别为activeconsumed。请注意在这种情况下,两个索引中的后缀匹配,这是执行zigzag合并连接的必要条件。

对于您当前拥有的索引,无法回答查询,因为

<datastore-index kind="Environment" ancestor="false">
    <property name="active" direction="asc" />
    <property name="consumed" direction="asc" />
</datastore-index>

没有creationDate作为后缀。

相关问题