Database Reference
In-Depth Information
> db.spreadsheets.ensureIndex({username: 1, updated_at: -1})
> db.spreadsheets.find({username: "Wallace"}).sort(
{updated_at: -1}).explain()
{
"clusteredType" : "ParallelSort",
"shards" : {
"shard-1-test-rs/arete:30100,arete:30101" : [
{
"cursor" : "BtreeCursor username_1_updated_at_-1",
"nscanned" : 801,
"n" : 801,
"millis" : 1,
}
]
},
"n" : 801,
"nscanned" : 801,
"numQueries" : 1,
"numShards" : 1
}
There are a couple of things to notice about this explain plan. The first is that the
query is directed to just a single shard. Since you've specified part of the shard key, the
query router can figure out which shard contains the relevant chunk. Realize then
that a sort doesn't necessarily imply a trip to all shards; when the shard key is included
in a sort query, the number of shards to query can often be pared down. In this case
you hit just one shard, but you can imagine similar queries hitting some number of
shards fewer than the total.
The second thing to notice from the explain plan is that the shard uses the {user-
name: 1, updated_at: -1} index to serve the query. This illustrates an important
point about how queries are processed in a sharded cluster. The shard key is used to
route the query to a given shard, but once there, each shard itself determines which
index to use to serve the query. Keep this in mind when designing queries and
indexes for your application.
9.3.2
Indexing
You just saw some examples of how indexed queries work in a sharded cluster. If
you're ever unsure about how a given query will resolve, use explain() to get the
answer. This is usually straightforward, but a few more points about indexes should be
kept in mind when running a sharded cluster. I'll enumerate them here:
Each shard maintains its own indexes. This should be obvious, but to be clear,
know that when you declare an index on a sharded collection, each shard builds
a separate index for its portion of the collection. For example, when you issued
the db.spreasheets.ensureIndex() command via mongos in the previous sec-
tion, each individual shard processed the index creation command individually.
1
It follows that the sharded collections on each shard should have the same
indexes. If this ever isn't the case, you'll see inconsistent query performance.
2
Search WWH ::




Custom Search