MongoDB Sharding-06-Enable Sharding For a Collection

shard is a replica set that contains a subset of a sharded collection (also known as chunks). A replica set consists of one or more mongod nodes that all maintain the same data set.

Replica sets provide high availability. Sharding allows us to partition the data across multiple replica sets and, as a result, boost the database resources.

The router (or mongos)acts as a query router for client applications, handling both read and write operations. The router caches the metadata from the config server in order to properly route requests to the relevant shard or shards.

The config servers store all the metadata for a sharded cluster. The metadata include state and organization for all data and components within the sharded cluster.

1. Find Documents Using Shard Key
mongos> db.mybooks.find({_id:10000})
{ "_id" : 10000, "name" : "book_number_10000", "author" : "author",
"description" : "this is a book" }
mongos> db.mybooks.find({_id:1})
{ "_id" : 1, "name" : "book_number_1", "author" : "author" } mongos> db.mybooks.find({_id:50000})
{ "_id" : 50000, "name" : "book_number_50000", "author" : "author",
"description" : "this is a book" }

2. Find Documents Using Shard Key and Check Number Of Shards Scanned and Used
mongos> db.mybooks.find({_id:10000}).explain()
{
"clusteredType" : "ParallelSort",
"shards" : {
"ReplicaSetNew3/localhost.localdomain:27031,localhost.localdomain:27 032,localhost.localdomain:27033" : [
{
"cursor" : "IDCursor",
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"indexOnly" : false,
"millis" : 0,
"indexBounds" : {
"_id" : [
[
10000,
10000]
]
},
"server" : "localhost.localdomain:27031"
}
]
},
"cursor" : "IDCursor",
"n" : 1,
"nscanned" : 1,
"nscannedObjects" : 1,
"millisShardTotal" : 0,
"millisShardAvg" : 0,
"numQueries" : 1,
"numShards" : 1,
"indexBounds" : {
"_id" : [[
10000,
10000
]
]
},
"millis" : 15
}

3.Find Documents Without Using Shard Key and Check Number Of Shards Scanned and Used
mongos> db.mybooks.find({name:"book_number_10000"}).explain()
{
"clusteredType" : "ParallelSort",
"shards" : {
"ReplicaSetNew1/localhost.localdomain:27011,localhost.localdomain:27 012,localhost.localdomain:27013" : [
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 9613,
"nscanned" : 9613,
"nscannedObjectsAllPlans" : 9613,
"nscannedAllPlans" : 9613,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 75,
"nChunkSkips" : 0,
"millis" : 18,
"server" : "localhost.localdomain:27011",
"filterSet" : false
}
],
"ReplicaSetNew2/localhost.localdomain:27021,localhost.localdomain:27 022,localhost.localdomain:27023" : [
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"nscannedObjectsAllPlans" : 0,
"nscannedAllPlans" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"server" : "localhost.localdomain:27021",
"filterSet" : false
}
],
"ReplicaSetNew3/localhost.localdomain:27031,localhost.localdomain:27 032,localhost.localdomain:27033" : [
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 40388,
"nscanned" : 40388,
"nscannedObjectsAllPlans" : 40388,
"nscannedAllPlans" : 40388,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 315,
"nChunkSkips" : 0,
"millis" : 54,
"server" : "localhost.localdomain:27031",
"filterSet" : false
}
]
},
"cursor" : "BasicCursor",
"n" : 1,
"nChunkSkips" : 0,
"nYields" : 390,
"nscanned" : 50001,
"nscannedAllPlans" : 50001,
"nscannedObjects" : 50001,
"nscannedObjectsAllPlans" : 50001,
"millisShardTotal" : 72,
"millisShardAvg" : 24,
"numQueries" : 3,
"numShards" : 3,
"millis" : 57
}

4. Exit From Mongo Shell
mongos> exit
bye

Also See:

Sharding in MongoDB

Leave a Reply