Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track MongoDB performance?

Tags:

Is there a way to track 'query' performance in MongoDB? Specially testing indexes or subdocuments?

In sql you can run queries, see execution time and other analytic metrics.

I have a huge mongoDB collection and want to try different variations and indexes, not sure how to test it, would be nice to see how long did it take to find a record.. (I am new in MongoDB). Thanks

like image 681
Stewie Griffin Avatar asked Dec 09 '10 15:12

Stewie Griffin


People also ask

How do I get MongoDB stats?

MongoDB: db.stats() method is used to return a document that reports on the state of the current database. The scale at which to deliver results. Unless specified, this command returns all data in bytes.

How does MongoDB check index performance?

Use the db. collection. explain() or the cursor. explain() method in executionStats mode to return statistics about the query process, including the index used, the number of documents scanned, and the time the query takes to process in milliseconds.


1 Answers

There are two things here that you'll likely be familiar with.

  1. Explain plans
  2. Slow Logs

Explain Plans

Here are some basic docs on explain. Running explain is as simple as db.foo.find(query).explain(). (note that this actually runs the query, so if your query is slow this will be too)

To understand the output, you'll want to check some of the docs on the slow logs below. You're basically given details about "how much index was scanned", "how many are found", etc. As is the case with such performance details, interpretation is really up to you. Read the docs above and below to point you in the right direction.

Slow Logs

By default, slow logs are active with a threshold of 100ms. Here's a link to the full documentation on profiling. A couple of key points to get you started:

Get/Set profiling:

db.setProfilingLevel(2); // 0 => none, 1 => slow, 2 => all db.getProfilingLevel(); 

See slow queries:

db.system.profile.find() 
like image 85
Gates VP Avatar answered Oct 12 '22 14:10

Gates VP