Mongodb – Choosing shard key with compound index field

mongodbsharding

I have a compound key with field articleId_1_host_1_timeStamp_1_isIndia_1_location_1.

Cardinality of fields is

  • articleId has 20000 different value
  • host has 17 value
  • timestamp is increasing field with time

I want to use articleId_1_host_1_timeStamp_1 as shard key.

Could you please suggest if I can use this as shard key with timestamp field in it? Also do I need to create a different index using these three fields or can I use the above compound key with shard key of of first three fields?

Best Answer

You should be able to use the index listed to cover the shard key. It is a super set of your shard key fields.

The shard key listed should be fine for distributing write load, given you don't expect any individual articleId/host pairs to take the bulk of your writes at a given point in time.

I would be concerned about this shard key for reads. In order to target a single shard for a query, you need to include the shard key values. My guess is your queries do not include timestamp. Without timestamp your queries will be sent to every shard which is inefficient. With scatter gather reads, your hamper your ability to scale reads by adding shards.