Database Reference
In-Depth Information
Analyzing a Specific Query with explain()
If you suspect a query is not performing as well as expected, you can use the explain() modifier to see exactly how
MongoDB is executing the query.
When you add the explain() modifier to a query, on execution MongoDB returns a document that describes
how the query was handled, rather than a cursor to the results. The following query runs against a database of blog
posts and indicates that the query had to scan 13,325 records to form a cursor to return all the posts:
$mongo
>use blog
> db.posts.find().explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 13235,
"nscannedObjects" : 13235,
"nscanned" : 13235,
"nscannedObjectsAllPlans" : 13235,
"nscannedAllPlans" : 13235,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
},
"server" : "Pixl.local:27017"
}
You can see the fields returned by explain() listed in Table 10-1 .
Table 10-1. Elements Returned by explain()
Element
Description
Cursor
Indicates the type of cursor created to enumerate the results. Typically, this is one of
the following: BasicCursor , a natural-order reading cursor; a BtreeCursor , which is
an index cursor; or a GeoSearchCursor , which is a query using a geospatial index.
indexBounds
Indicates the min/max values used on an indexed lookup.
nScanned
Indicates the number of index entries scanned to find all the objects in the query.
nScannedObjects
Indicates the number of actual objects that were scanned, rather than just their
index entries.
n
Indicates the number of items on the cursor (that is, the number of items to be
returned).
millis
Indicates the number of milliseconds it took to execute the query.
nscannedAllPlans
Indicates the nscanned value of all the attempted plans.
nscannedObjectsAllPlans
Indicates the nScannedObjects value of all the attempted plans.
( continued )
 
Search WWH ::




Custom Search