CosmosDb Mongo - collection with shardkey,shardkey 查询慢?

CosmosDb Mongo - collection with shardkey, slow query by shardkey?

我有一个 CosmosDb collection 和 Mongodb。 这是一个客户数据库,ShardKey其实就是CustomerId。 我的 collection 有 200000 条记录,并且有 e-mail 和 customerid 的组合索引。

客户示例:

{
"CustomerId" : "6a0f4360-d722-4926-9751-9c7fe6a97cb3",
"FirstName" : "This is my company first name",
"LastName" : "This is my company last name",
"Email" : "6a0f4360-d722-4926-9751-9c7fe6a97cb3@somemail.com",
"Addresses" : [
    {
        "AddressId" : "54e34da9-55fb-4d60-8411-107985c7382e",
        "Door" : "11111",
        "Floor" : "99",
        "Side" : "B",
        "ZipCode" : "8888",
    }
]

}

我觉得奇怪的是,如果我通过电子邮件查询它会花费 7000RU(这太多了 - 至少数据资源管理器告诉我的是......)但是如果我通过 CustomerId 查询,它花费或多或少相同RU...

我的问题是:

E-mail的查询示例:

{ "Email" : { $eq: "3f7da6c3-81bd-4b1d-bfa9-d325388079ab@somemail.com" } }

CustomerId 的查询示例:

{ "CustomerId" : { $eq: "3f7da6c3-81bd-4b1d-bfa9-d325388079ab" } }

另一个问题,我的索引同时包含 Email 和 CustomerId。有没有办法让我查询 e-mail 和 return 仅 CustomerId,例如?

Shoudn't both operations spend less RUs than this, specially by CustomerId?

CustomerId 是您的分片键(又名分区键),它有助于将具有相同 CustomerId 值的文档分组以存储在同一逻辑分区中。此分组在 pin-point GET/SET 调用 Cosmos 期间使用,但不在查询期间使用。因此,您需要明确地在 CustomerId 上建立索引。

此外,由于您拥有的索引是 CustomerIdEmail 上的复合索引,一次仅对这些字段中的一个进行查询将导致在中执行扫描为了取回结果。因此,每个查询的 RU 费用较高且 RU 费用相似。

Another question, my index contains both Email and CustomerId. Is there any way for me to query by e-mail and return only CustomerId, for example?

首先,为了在 Email 上进行最佳查询,您需要在 Email 上单独创建一个索引。此后,您可以使用 project feature of Mongo 在响应中仅包含某些字段。

像这样-

find({ "Email" : { $eq: "3f7da6c3-81bd-4b1d-bfa9-d325388079ab@somemail.com" } }, { "CustomerId":1 })