Learn all about MongoDB Database Profiler for Monitoring Database Queries and Performance:
In this Free MongoDB training series, we learned about MongoDB Performance in our previous tutorial.
In this tutorial, we will learn all about MongoDB Database Profiler in detail.
Database profiler is used to collect information regarding the queries which are executed on an individual database instance.
If you are working with an enterprise level application and if you have been simultaneously executing queries then maybe in some queries you have to face a deadlock.
In order to identify the query in which you are facing the deadlock or any kind of issues, there is a feature called profiler. MongoDB is also providing this feature to record the log of an individual query which is executed. These logs record all the crud operations along with configuration and management controls.
By default, all data is recorded within the system.profiles collection within the MongoDB admin instance.
The profiler is disabled due to the high consumption of memory by default. There are three different levels of the profiler to record the information regarding the queries and you can easily set any level of profiler on any instance of MongoDB.
Table of Contents:
Enable and Configure Profiling for Databases
Database profiler is activated by the profile command with the help of the mongo shell. Whenever you are activating profiler to log the record of query execution, then you have to mention the level of profiling. With the help of the following code, we are going to enable profiling for MongoDB.
Syntax
db.setProfilingLevel(LEVEL)
Code
db.setProfilingLevel(2)
Figure 1: In Mongo Shell
Figure 2: In Robo 3T
In the above image, you can observe that there are four outcomes. In the first field, it is showing the previously used profile level and the last field is indicating the success of the operation.
Check the Level of Profiling
To preview the current level of the profiler, you have to use the following code.
Code
db.getProfilingStatus()
It will show you the current and previously used profiler status.
Figure 3: In Mongo Shell
Figure 4: In Robo 3T
- was the current level of profiling.
- slowms field shows the operating time limit in milliseconds.
- SampleRate shows the percentage of slow operations to be profiled.
To get only the profiler level, you can use the db.getProfilingLevel () in the mongo shell.
Code
db.getProfilingLevel()
Figure 5: In Mongo Shell
Figure 6: In Robo 3T
Deactivate Profiling
If you want to deactivate the profiler, you can use the following code to stop logging the query execution information.
Code
db.setProfilingLevel(0)
Figure 7: In Mongo Shell
Figure 8: In Robo 3T
Overhead Profiler
When you are logging the record of query execution or you are using the profiler, then it would likely affect the performance of query execution. By default, the profiler collection has 1 MB as a memory to store the information.
If you have a huge application and a lot of transactional data, then it will be overhead to store a lot of information as a profiler.
Change the Size of the system.profile Primary Collection
Before you are going to change system.profiles collection size, you must do the following things:
- Deactivate profiling
- Drop the collection system.profile
- Create a new.profile system collection
- Reactivate profiling
Code
db.setProfilingLevel(0) db.system.profile.drop() db.createCollection( "system.profile", { capped: true, size:4000000 } ) db.setProfilingLevel(1)
Figure 9: In Mongo Shell
Figure 10: In Robo 3T
Conclusion
MongoDB database profiler is used to monitor the queries and their performance within the MongoDB instance. We can monitor queries on different levels of profiling as we discussed in the previous tutorial.
In this tutorial, we have successfully learned how to switch the level of profiling and how we can disable them as per our requirement. We can also set up the required threshold to store the profiler record.
Our upcoming tutorial will explain to you about User creation and assigning roles in MongoDB!!