如果我们已经将分区键设置到 FeedOption 中,查询是否应该包含分区键?

duy*_*duy 5 linq performance azure-cosmosdb

我正在使用带有分区键 =“deviceId”的文档数据库。下面的2个代码有什么不同:

var fo = new FeedOption{ PartitionKey= new PartitionKey("A1234") };
var partitionKeyInQuery= dbClient.CreateDocumentQuery(d => d.deviceId = "A1234" and d.type==1, fo);
var noPartitionKeyInQuery = dbClient.CreateDocumentQuery(d => d.type==1, fo);
Run Code Online (Sandbox Code Playgroud)

在 FeedOption 中应用 PartitionKey 时,我应该在 WHERE 子句中添加“deviceId”吗?

Pep*_*ova 3

我相信性能上没有区别。RequestCharge 是相同的,where 子句使查询分区特定,即消除跨分区查询。

从文档中:

查询分区容器

当您查询分区容器中的数据时,Cosmos DB 会自动将查询路由到与筛选器中指定的分区键值(如果有)对应的分区。例如,此查询仅路由到包含分区键“XMS-0001”的分区。

// Query using partition key
IQueryable<DeviceReading> query = client.CreateDocumentQuery<DeviceReading>(
    UriFactory.CreateDocumentCollectionUri("db", "coll"))
    .Where(m => m.MetricType == "Temperature" && m.DeviceId == "XMS-0001");
Run Code Online (Sandbox Code Playgroud)

https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-partition-data