2

I am creating a collection which stores JSON object using MongoDB. I am stuck in Sharding part. I have an Case ID,Customer ID and Location for each of the record in the collection

The Case ID is a 10 digit number (only number and no alphabets).

The CustomerID is a combination of customer name and case ID.

The location is a 2dsphere value and I am expecting a location of different distinct values.

In addition to this I have customer name and case description to the record. All my search queries have search criteria of either Case ID, CustomerID or location.

Given this scenario, Can I create a compound key based on all these three values (CaseID, CustomerID and location). I believe this gives a high cardinality and easy to retrieve the records.

Could any one please suggest me if this is a good approach as I am not finding a compound shard key comprising of three values.

Thanks for your time and let me know if you need any information

4

1 回答 1

13

首先要考虑的是是否需要分片。如果您的数据集适合单个服务器,则从非分片部署开始。如果需要,以后可以轻松无缝地将其转换为分片集群。

假设您确实需要分片,您对分片键的选择应基于以下标准:

  1. 基数- 选择一个不限于少数可能值的分片键,以便 MongoDB 可以在集群中的分片之间均匀分布数据。
  2. 写分布——选择一个在集群中的分片之间均匀分布写操作的分片键,以防止任何单个分片成为瓶颈。
  3. 查询隔离- 选择包含在最频繁查询中的分片键,以便这些查询可以有效地路由到保存数据的单个目标分片,而不是广播到所有分片。

您提到您的所有查询都包含案例 ID、客户 ID 或位置,但没有描述您的用例。举个例子,假设您最常见的查询是:

  • 检索客户案例
  • 检索给定客户的所有案例

在这种情况下,一个好的片键候选将是 (name, caseID) 上的复合片键(以及相应的复合索引)。考虑这是否满足上述标准:

  1. 基数 - 每个文档对分片键都有不同的值,因此基数非常好。
  2. 写入分布 - 所有客户的案例分布在所有分片中。
  3. 查询隔离:
    • 要检索特定案例,查询中应包含名称和案例 ID。此查询将被路由到保存文档的特定分片。
    • 要检索给定客户的所有案例,请在查询中包含名称。因此,此查询包含分片键的前缀,因此也将有效地仅路由到保存与查询匹配的文档的特定分片。

请注意,您不能将地理空间索引用作分片键索引的一部分(如此所述)。但是,如果使用其他字段作为分片键,您仍然可以在分片集合上创建和使用地理空间索引。例如,使用上面的分片键:

  • 还包括客户名称的地理空间查询将针对相关分片。
  • 不包括客户姓名的地理空间查询将被广播到所有分片(“分散/收集”查询)。

可以在此处找到有关分片键注意事项的其他文档。

于 2014-01-08T16:24:22.413 回答