You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行跨分区查询时提示‘Cross partition query is required but disabled’求助

Fixing 'Cross partition query is required but disabled' in Cosmos DB Pagination Queries

Hey there! Let's break down why you're hitting this error and how to fix it quickly.

What's causing the error?

The Cross partition query is required but disabled message means your query is trying to fetch results spread across multiple Cosmos DB partitions, but your query client isn't configured to allow cross-partition access. When you use HasMoreResults and ExecuteNextAsync for pagination, if remaining results live in other partitions, the client throws this error—cross-partition queries are disabled by default as a performance safeguard.

Solutions to resolve this

1. Enable cross-partition query (quick fix)

If you can't narrow your query to a single partition, explicitly enable cross-partition queries when creating your query iterator. Here's how to adjust your code based on your Cosmos DB SDK version:

For SDK v3+ (current recommended version)

Add a QueryRequestOptions with EnableCrossPartitionQuery set to true when initializing your query:

// Adjust your query creation to include cross-partition support
var query = container.GetItemQueryIterator<T>(
    yourQueryDefinition, // Replace with your actual QueryDefinition
    requestOptions: new QueryRequestOptions { EnableCrossPartitionQuery = true }
);

// Your existing pagination logic stays the same
List<T> results = new List<T>();
while (query.HasMoreResults)
{
    results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
For SDK v2 (older version)

Use FeedOptions instead of QueryRequestOptions:

var query = client.CreateDocumentQuery<T>(
    collectionUri,
    yourQueryDefinition,
    new FeedOptions { EnableCrossPartitionQuery = true }
).AsDocumentQuery();

// Rest of your pagination code remains unchanged

2. Target a specific partition (better for performance)

Cross-partition queries are slower because they scan all partitions. If your use case allows it, specify the partition key when creating the query iterator. This limits the query to only the partition holding your data, eliminating the error and improving performance:

// Replace "your-partition-key-value" with your actual partition key value
var query = container.GetItemQueryIterator<T>(
    yourQueryDefinition,
    partitionKey: new PartitionKey("your-partition-key-value")
);

// Your existing pagination loop works as expected

Key Note

Always prioritize targeting a specific partition if possible. Cross-partition queries should only be a fallback when you truly need to fetch data across multiple partitions.

内容的提问来源于stack exchange,提问作者Advisor Magwaza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 13:22:47