Profiling in MongoDB

In this tutorial, I will let you know profiling in MongoDB, why is it so important, and when should we enable profiling.
Profiling in MongoDB
Profiling in MongoDB plays a big role in query optimization, find the queries which is not using indexing, it help us to identify the slow queries and fix them. We can make our application more responsive.
Profiling Level
There are three type of profiling level in MongoDB.
+------------+----------+-------------------------------------------------------------------------------------------------------------------------+
| Parameter  | Type |   Description                                                                                                           |
+------------+----------+-------------------------------------------------------------------------------------------------------------------------+
| level      | integer |   Specifies a profiling level, which is either 0 for no profiling, 1 for only slow operations, or 2 for all operations. |
| slowms     | integer |   Optional. Sets the threshold in milliseconds for the profile to consider a query or operation to be slow.             |
+------------+----------+-------------------------------------------------------------------------------------------------------------------------+
To check current profiling level in MongoDB
> db.getProfilingLevel()
0
db.getProfilingStatus()
{ "was" : 0, "slowms" : 100 }
In above, we can see profiling level as 0, which I have already explained different profiling level and their meaning.
To Set Profiling level 
To enable profiling use the below command in the database.
> use db_name
switched to db db_name
db.setProfilingLevel(1,200)
{ "was" : 0, "slowms" : 100, "ok" : 1 }
In above, I have setup profiling level to 1 and treating queries as slow which took more than 200ms.
We can also set profiling level to 2 and print all the queries whatever comes to MongoDB as:-
>db.setProfilingLevel(2)
Note:- Please be careful while enabling Profiling, as it impact the performance of Database, always disable the profiling once find the queries which you have to optimize or check.

No comments:

Post a Comment