Create appropriate shard key

Hello guys. I haven’t find information how to create appropriate shard key.
My collection can looks like this:

{
 "id":1,
 "first_name": "test",
 "last_name":"test"
}

or look like this:

{
 "id":"some@gmail.com",
 "first_name": "test",
 "last_name":"test"
}

So i haven’t some integer value that i can use for shard key.
For example, if my collection of contacts consist of uuid id how can i make ranged shard key?

Hi @111891 ,

Choosing shard keys is a question of both :

  1. How data Will be distributed and inserted (we don’t want skew of many documents on one shard and not the others, and we don’t want one shard to get all traffic each time)
  2. What are the most important queries and how to avoid a distrebuted queries to all shard because the shard key or at least the prefix is not present as a predict…

Now in your case are the queries on “id” field? Are those range queries or specific id queries?

Thanks
Pavel

Hi Pavel. Thank you for reply. My question is can i use random integer value from 0 to 100000000 for example as shard key and in queries specify
table.whereBetween(‘id’, [0,1000000000]. In my case i dont know what data will be in my collection because i use multitenant architecture and separate different organizations using different collections so i dont know what data will be in collection. But data size can be very large and i want to increase query performance using sharding mechanism

Hi @111891 ,

If the cardinality of this id value is high than MongoDB will try to distrebute it equally across shards. Of course in range queries at some point you might need to scan many shards because all values will not be under one shard.

You can try to shard based on

{ id : 1} 

At the moment. Version 5.0+ has an online resharding feature available. I recommend using this server version and change the shard key if performance is not setisfying.

Thanks
Pavel

Basically you dont understand what is my question. So my question is can i use random integer field as shard key? Instead of some customer_id i wanna use random integer value.

I havent any id field

Hi @111891 ,

Technically its possible, if its optimal depand on application queries and load testing…