56

MongoDB Explain – Using PMM-QAN for MongoDB Query Analytics

 5 years ago
source link: https://www.tuicool.com/articles/hit/zIrQFbZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this blog post, we will walk through PMM-Query Analytics for MongoDB. We will see how to analyze MongoDB query performance; review the initial parameters that we need to check; and find out how to compare MongoDB query performance with and without indexes with the help of EXPLAIN plan.

The Percona Monitoring and Management QAN (PMM-QAN) dashboard helps DBAs and Developers to analyze database queries and identify performance issues more easily. Sometimes it is difficult to find issues by just enabling the profiler and tracking through mongo shell for all the slow queries.

Test Case Environment

We configured the test environment with PMM Server before we ran the test:

PMM Version:<span class="s1">1.11.0</span>
MongoDB Version: 3.4.10
 
MongoDB Configurations:
3 Member Replicaset (1 Primary, 2 Secondaries) 
ns: "test.pro"
Test Query: Find Test case: (with and without Index) 
Indexed Field: "product"
Query: db.pro.find({product:"aa"})
Query count: 1000 
Total count: 20000

Please Note:We have to enable profiler in the MongoDB environment to reflect the metrics in the QAN page. The QAN dashboard lists multiple queries, based on the threshold we have set in profiler. For this demonstration, we have set profiling level to 2 to get the query reflected in the dashboard.

QAN Analysis

Let’s analyze comparisons of the plans that were collected before and after the index was added to the collection.

Query used:

db.pro.find({product:"aa"})
 

The QAN dashboard, lists the FIND query for the “pro” collection:

f2YJrav.png!web

Query Time

After selecting this specific query, we will see its details just below the list.

Review parameter: “Query Time”

Without Index

ZJfMrqR.png!web

Here Query Time=18ms, we will check for the query count, docs returned and docs scanned in detail in the explain plan.

With Index

EZ7JniQ.png!web

Now the Query Time=15ms, we have improved the query by creating an index, and MongoDB is no longer scanning the whole collection.

EXPLAIN PLAN

Analysis of the query from the QAN EXPLAIN Plan: You can use the toggle button “Expand All” to check the complete execution stats and plans, as this in case of the “test” database

ERFFryr.png!web

COMPARISON OF PERFORMANCE

Here we make a comparison of query performance with and without the index, and take a look at the basics that need to be checked before and after index creation:

Without Index:

yE7bmyR.png!web

Stage =”COLLSCAN”, this means that MongoDB scans every document in order to fulfil the find query, so you need to create an index to improve query performance

With Index:

yYvaquA.png!web

Stage=”IXSCAN”, indicates that the optimizer is not scanning the whole document, but scanned only the indexed bound document

Without Index:

JzEnmmm.png!web

It scanned whole documents i.e. docsExamined:20000 and return nReturned:1000 .

With Index:

36NFbmi.png!web

MongoDB uses the index and scans only the relevant documents .i.e. docsExamined:1000 and return nReturned:1000. We can see the performance improvement from adding an index.

This is how the query behaves after index creation. We can identify exactly which index is being used with QAN. We can discover whether that index is useful or not, and other performance related events, all with an easy UI.

As this blog post is specific to Query Analysis for MongoDB, QAN graphs and their attributes can be accessed fromhere, an excellent blog post written by my colleague Vinodh.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK