Friday, February 27, 2015

MongoDB Query Plan and Distributed Queries

Mongodb query optimizer processes query and chooses the most efficient query plan for query. The results are buffered and query plan is cached, so next time when the query is fired cached query plan is used.

Query optimizer also evaluates query plan on timely basis based on the conditions like mongo restart, add or drop index, reIndex etc.

db.collection.getPlanCache() method provide interface to view query plan information and clear plan.

Distributed Queries : Sharded cluster allow you to partition data based on the shard key and mongos router based on the config metadata information will route the query to specific shard node only.

This is possible only if query includes shard key, in case if query does not include shard key, mongos router will route the query on all cluster.

Replica sets uses read preferences to determine how to route query. Read preferences can be per connection based or per operation based.

Default read preference are read on primary, nearest is on secodary with minimum n/w latency, secondaryOnly is read only through secondary and error if secondaries are not available. 

Secondary is when read from secondary and if all are not available read from primary.

No comments:

Post a Comment