Thursday, October 4, 2018

Mongo DB profiling can be done for checking how much time which query is taking.

Mongo DB profiling can be done for checking how much time which query is taking.

Profiling Levels
The following profiling levels are available:
Level
Description
0
The profiler is off and does not collect any data. This is the default profiler level.
1
The profiler collects data for operations that take longer than the value of slowms.
2
The profiler collects data for all operations.
db.setProfilingLevel(0);
db.setProfilingLevel(1);
db.setProfilingLevel(2);




From the above image we came to know the following.

1.       While getting the more than 101 records , First 101 records will be return by Mongo  and to fetch the remaining records , getmore command will be issued and will get the complete records based on the condition.(if still more records are there (16MB initial getmore))
2.       To get 10,000 records its takes only 48 mills.
Note : system.profile collection is not normal collection, it is capped collection . so that we can not delete the records manually .
If you want to delete records then we need to do the following way.
db.system.profile.drop();
db.createCollection("system.profile", {capped: true, size: 10 * 1024 * 1024}); 

A few show tricks to find slow queries in mongodb

Enable profiling

First, you have to enable profiling
> db.setProfilingLevel(1)
Now let it run for a while. It collects the slow queries ( > 100ms) into a capped collections, so queries go in and if it's full, old queries go out, so don't be surprised that it's a moving target...

Histogram of the slowest collections (collections with the slowest queries) - number of queries per collection

This presents a histogram of slow collections
> db.system.profile.group({key: {ns: true}, initial: {count: 0}, reduce: function(obj,prev){ prev.count++;}})

Histogram of the slowest collections (collections with the slowest queries) - number of millies spent in each collection

> db.system.profile.group({key: {ns: true}, initial: {millis: 0}, reduce: function(obj, prev){ prev.millis += obj.millis;}})

Find the most recent slow query

> db.system.profile.find().sort({$natural: -1}).limit(1)

Find the single slowest query in the capped system.profile collection right now


> db.system.profile.find().sort({millis: -1}).limit(1)

No comments:

Post a Comment

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...