gpt4 book ai didi

azure-cosmosdb - 文档计数查询忽略 PartitionKey

转载 作者:行者123 更新时间:2023-12-02 19:19:30 25 4
gpt4 key购买 nike

我希望获得所选分区中所有文档的数量。但是,以下代码将返回集合中所有文档的计数并且成本为 0 RU。

    var collectionLink = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

string command = "SELECT VALUE COUNT(1) FROM Collection c";

FeedOptions feedOptions = new FeedOptions()
{
PartitionKey = new PartitionKey(BuildPartitionKey(contextName, domainName)),
EnableCrossPartitionQuery = false
};

var count = client.CreateDocumentQuery<int>(collectionLink, command, feedOptions)
.ToList()
.First();

添加 WHERE c.partition = 'blah'查询的子句将起作用,但在集合中有 11 个文档时花费 3.71 RU。

为什么上面的代码片段会返回整个集合的计数,是否有更好的解决方案来获取所选分区中所有文档的计数?

最佳答案

If the query includes a filter against the partition key, like SELECT * FROM c WHERE c.city = "Seattle", it is routed to a single partition. If the query does not have a filter on partition key, then it is executed in all partitions, and results are merged client side.



您可以从这个官方 doc 中检查 SDK 执行的逻辑步骤。当我们向 Azure Cosmos DB 发出查询时。

If the query is an aggregation like COUNT, the counts from individual partitions are summed to produce the overall count.



所以当你只使用 SELECT VALUE COUNT(1) FROM Collection c ,它在所有分区中执行,结果合并到客户端。

如果您想获取所选分区中所有文档的数量,只需添加 where c.partition = 'XX'筛选。

希望对你有帮助。

关于azure-cosmosdb - 文档计数查询忽略 PartitionKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com