EnableScanInQuery不再适用于新的CosmosDB容器

时间:2019-10-07 11:27:22

标签: azure azure-cosmosdb azure-cosmosdb-sqlapi

我对cosmos-db索引策略和隐式全扫描感到困惑。

我的最终目标是:

  • 防止对未索引属性进行意外全扫描
  • 仅索引明确指定的属性

让我们说我有一张这样的桌子:

{
  "id": "transactions",
  "indexingPolicy": {
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
      {
        "path": "/transactionId/?"
      },
      {
        "path": "/createdOn/?"
      }
    ],
    "excludedPaths": [
      {
        "path": "/*"
      },
      {
        "path": "/\"_etag\"/?"
      }
    ]
  },
  "partitionKey": {
    "paths": [
      "/chargePointId"
    ],
    "kind": "Hash"
  }
}

我认为这意味着我有2个一致的索引(在transactionId和createdOn列上)。 Azure门户不允许我指定索引类型:它“接受”更改,但是在重新加载页面时,所有更改都消失了。

现在,我对禁用了全扫描的不存在的列执行查询,假设它会因以下错误而失败:An invalid query has been specified with filters against path(s) excluded from indexing. Consider adding allow scan header in the request.

但是,这不会发生。它可以正常工作并在控制台中打印00:00:00

var policy = new ConnectionPolicy()
{
    ConnectionMode = ConnectionMode.Gateway
};

var client = new DocumentClient(host, key, policy);

var queryText = "select * from c where c.asdasdasd > '2'";

var query = client.CreateDocumentQuery(
     UriFactory.CreateDocumentCollectionUri("transactions", "transactions"),
     queryText,
     new FeedOptions
     {
         PopulateQueryMetrics = true,
         EnableScanInQuery = false,
         EnableCrossPartitionQuery = true,
     }
).AsDocumentQuery();

var result = await query.ExecuteNextAsync();
var metrics = result.QueryMetrics;

Console.WriteLine(metrics.Single().Value.QueryEngineTimes.IndexLookupTime);

(代码摘自本指南https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-query-metrics

我正在谈论的表是最近创建的(〜几周前)。我在另一个很久以前创建的数据库帐户中也有一个表。如果我尝试在该表上执行相同的操作-会失败,就像我期望的那样。

我没有发现帐户或表手臂模板(从Azure门户导出)的任何差异。

为什么新表不会失败? 还是默默地为事物编制索引,还是EnableScanInQuery不再被新表所尊重?

1 个答案:

答案 0 :(得分:0)

我来自CosmosDB工程团队。我们将逐渐取消对EnableScanInQuery的支持,因为并非对每个可能的查询(例如,来自r的SELECT *)都统一实施该支持。此外,当EnableScanInQuery设置为false时,仍然允许进行部分扫描(即使查询中选择最少的谓词之一满足大量文档并可以从索引提供服务,我们也可以接受该查询,即使它可以有效地扫描)。对于今年年初开始的新集装箱,支持正在逐步取消。优化查询以避免扫描的推荐方法是检查查询执行指标,以确定是否需要针对索引策略进行任何优化。

这实际上意味着,无论EnableScanInQuery是什么,查询都可能降级以自动扫描以提供结果,并且对于不支持此选项的容器,用户将看不到任何错误消息。