Performance Tools and Techniques / Tools for Analyzing Slow Queries

Code Recap: Performance Tools for Profiling QueriesCode Recap: Tools for Analyzing Slow Queries

Using the grep Command to Parse MongoDB Log Files

The grep command is used for searching text within files or streams based on patterns. It scans specified files or input and returns lines that match the given pattern or regular expression. With its ability to filter large amounts of text, grep is helpful for log analysis, debugging, and extracting specific data.

grep "Slow query" /path/to/mongo.log

Using jq to Pretty Print JSON Documents

jq is a lightweight and powerful command-line JSON processor. It lets you parse, transform, filter, and retrieve specific data from JSON input. jq makes it easy to manipulate JSON data through its expressive query syntax, enabling users to focus only on the fields or elements they need.

This example uses grep to search for lines with "Slow query" in the mongo.log file and pipes the output to jq for pretty printing the JSON documents.

grep "Slow query" /path/to/mongo.log | jq '.'

This example uses grep to search for lines with "Slow query" in the mongo.log file and jq filters these lines to include only those where the workingMillis attribute in the JSON data is greater than 200.

grep "Slow query" /path/to/mongo.log | jq  'select(.attr.workingMillis > 200)'

This example uses grep to search for lines with "Slow query" in the mongo.log file and jq filters these lines to include only those where the namespace (ns) matches db.collection. It retrieves specific fields such as ns, command, planSummary, keysExamined, etc., from the JSON object using the {...} syntax to create a structured view of the selected data.

grep "Slow query" | jq 'select(.attr.ns == "db.collection") | {
  ns: .attr.ns,
  command: .attr.command,
  planSummary: .attr.planSummary,
  keysExamined: .attr.keysExamined,
  docsExamined: .attr.docsExamined,
  nreturned: .attr.nreturned,
  workingMillis: .attr.workingMillis,
  durationMillis: .attr.durationMillis
}' /path/to/log/file

Using an Explain Plan

Below is the code used to run the explain() method with mongosh to return an explain plan. An explain plan is a report that gives us information about possible plans for executing a query, including the winning plan that MongoDB selects.

Code

explain() Method

Use the explain() method, chained with the find() method to return the explain plan in mongosh.

Example Query

This query is used to retrieve documents from the listingsAndReviews collection for all documents that match on the address.market field value of "NY" with a price less than $1000. The results are then sorted by the price in descending order.

The explain() method is chained to this query with verbosity level set to executionStats. This will return an explain plan document in the terminal that will include execution stats for the winning plan.

db.listingsAndReviews.explain("executionStats").find(
{ 
  address: { market: "NY" }, 
  price: { $lt: 1000 }
}).sort({ price: -1 });