Query analysis in MongoDB
Analyzing queries is a very important aspect to assess the effectiveness of Database and the effectiveness of the designed index. We will explore the two most frequently used queries, $ explain and $ hint .
Use $ explain in MongoDB
The $ explain operator provides information about queries, indexes used in queries, and some other statistics. This operator is quite useful when analyzing how your indexes have been optimized.
In the previous chapter, we created an index for users collection on the gender and user_name fields with the following query:
> db . users . ensureIndex ({ gender : 1 , user_name : 1 })
Now, you use $ explain on the following query:
> db . users . find ({ gender : "M" },{ user_name : 1 , _id : 0 }). explain ()
The above query will result in the analysis as follows:
{
"cursor": "BtreeCursor gender_1_user_name_1",
"isMultiKey": false,
"n": 1,
"nscannedObjects": 0,
"nscanned": 1,
"nscannedObjectsAllPlans": 0,
"nscannedAllPlans": 1,
"scanAndOrder": false,
"indexOnly": true,
"nYields": 0,
"nChunkSkips": 0,
"millis": 0,
"indexBounds": {
"gender": [
[
"M",
"M"
]
],
"user_name": [
[
{
"$ minElement": 1
},
{
"$ maxElement": 1
}
]
]
}
}
Look at the fields in this result set:
The true value of indexOnly indicates that this query has used indexing.
The cursor field determines the type of pointer used. The BTreeCursor type indicates that an index is used and also provides the name of the index used. BasicCursor indicates that a full scan has been performed without using any indexes.
Number n indicates the number of documents that have been matched in the result returned.
nscannedObjects shows the total number of scanned documents.
nscanned indicates the total number of documents or indexes that have been scanned.
Use $ hint in MongoDB
The $ hint operator Query Query to use the specified index to run a query. It is especially useful when you want to check the performance of a query with different indexes. For example, the following query identifies the index on the gender fields and user_name to be used for this query:
> db . users . find ({ gender : "M" },{ user_name : 1 , _id : 0 }). hint ({ gender : 1 , user_name : 1 })
To analyze the above query, use the operator $ explain in MongoDB:
> db . users . find ({ gender : "M" },{ user_name : 1 , _id : 0 }). hint ({ gender : 1 , user_name : 1 }). explain ()
According to Tutorialspoint
Previous post: Covered Query in MongoDB
Next lesson: Atomic Operation in MongoDB
You should read it
May be interested
- MongoDB malicious code attacks more than 26,000 victims in a weekmalware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
- Learn about security features and authentication in MongoDBin the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
- ObjectId in MongoDByou have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
- Text Search in MongoDBstarting with version 2.4, mongodb started supporting text indexes to search within the string content.
- Action Query in Action 2016action queries are queries that perform actions on data, can add, change or delete records.
- Shard in MongoDBsharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
- Learn about Java Driver in MongoDBin the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
- Install MongoDBinstructions for installing mongodb on windows.
- Multiple choice questions have a Query optionquery is a language used in databases and information systems. the following is an invitation to test your knowledge with network administrator through multiple-choice questions below.
- Map Reduce in MongoDBin mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.