宇宙数据库 SQL;Feed 选项上的分区键与分区键上的查询?

Odd*_*eif 2 azure-cosmosdb azure-cosmosdb-sqlapi

在 .net core sdk FeedOptions 中提供分区键与将其作为查询条件有什么区别?例如,如果您想列出给定分区键中的所有项目。您应该在条件中使用提要选项还是分区键?

标准中的分区键

SELECT * FROM c where c.PartitionKey = "some partition key"
Run Code Online (Sandbox Code Playgroud)

.net core 与 feedoptions

var allDocs = await (from d in client.CreateDocumentQuery<Document>(UriFactory.CreateDocumentCollectionUri(databaseId, "TestCollection"), 
                     new FeedOptions { PartitionKey = new PartitionKey("some partition key") })
                     select d)
                    .AsDocumentQuery<Document>().ExecuteNextAsync<Document>();
Run Code Online (Sandbox Code Playgroud)

当我创建自定义索引策略(其中分区键被排除在索引之外)时,出现了问题。因此,在查看 RU/s 成本时,它似乎在排除时进行扫描,而不是在使用默认索引策略时进行索引查找。

这是否意味着如果您需要跨分区查询,您需要索引分区键,如果您只需要在给定分区键内查询,您可以排除它 - 假设您在 FeedOptions 中给出分区键?

Kri*_*ram 5

我来自 CosmosDB 工程团队。

您的帖子中提出了两个相互正交的问题:

  1. 我们是在 feedOptions 中还是在查询中指定 PartitionKey?

这应该不重要。在 FeedOptions 或查询本身中指定它将有助于它路由到正确的分区来执行。

  1. 我们是否需要将分区键添加到索引策略中以实现高效的查询执行?

您应该始终将分区键添加到索引策略中。这确保在查询执行期间,利用索引来为查询中指定的分区键选择文档。