How should you choose a shard key?

Shard Key Considerations  
  • Consists of: one or more fields in a document
  •  
  • The key should have these properties:
  •  
  • Cardinality: ability to partition data into "chunks"
  • Example: phone_number: CC-AAA-XXX-NNNN
  • Usually unique per user
  •  
  • Write Scaling: one part which changes quickly, another part which changes less frequently
  • Example: create_date: Y-m-d H:i:s
  • For sequential writes, Y-m-d H parts change less frequently, causing data to end up in one chunk
  •  
  • Query Isolation: if the key is too random, reads will not be able to isolate the shard, degrading performance
  • Example: postal_code + customer_id
NOTE: you can also create a hashed index on a hash key, but only if the key is a single field.





>>
 


+ o -