为什么不总是使用 EnableCrossPartitionQuery

wal*_*ark 5 azure-cosmosdb

如果我的 cosmos DB 有多个分区,是否有任何理由不设置EnableCrossPartitionQuerytrue

我知道如果运行可能会命中多个分区的查询,这是必要的。但是,如果查询使用有效的分区键并且肯定只会命中一个分区,是否会因为我将该标志设置为 true 而导致性能损失或增加成本?

Jay*_*ong 2

但是,如果查询使用有效的分区键并且肯定只会命中一个分区,是否会因为我将该标志设置为 true 而导致性能损失或增加成本?

据我所知,您需要为分区集合设置分区键,即使您仍然将其设置EnableCrossPartitionQuery为true,成本也不会改变。因为该请求仅扫描您已经设置的特定分区。我做了一个样本测试并尝试验证它。

    FeedOptions feedOptions = new FeedOptions();
    PartitionKey partitionKey = new PartitionKey("A");
    feedOptions.setPartitionKey(partitionKey);
    feedOptions.setEnableCrossPartitionQuery(true);

    FeedResponse<Document> queryResults = client.queryDocuments(
            "/dbs/db/colls/part",
            "SELECT * FROM c",
            feedOptions);

    System.out.println("Running SQL query...");
    for (Document document : queryResults.getQueryIterable()) {
        System.out.println(String.format("\tRead %s", document));
    }

    System.out.println(queryResults.getRequestCharge());
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想也许你不必为这个问题而苦苦挣扎。EnableCrossPartitionQuery仅当分区集合的查询范围不限于单个分区键值时才需要使用该选项。如果您知道具体的分区键,则无需设置EnableCrossPartitionQuery