我正在使用带有分区键 =“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);
在 FeedOption 中应用 PartitionKey 时,我应该在 WHERE 子句中添加“deviceId”吗?
最佳答案
我相信性能上没有区别。 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");
https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-partition-data
关于performance - 如果我们已经将分区键设置到 FeedOption 中,查询是否应该包含分区键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52050064/