How to optimize MongoDB performance (five simple steps)

  • 2020-05-17 06:50:58
  • OfStack

MongoDB 1 is currently the most popular NoSQL, and according to the latest DB-Engines Ranking ranking, MongoDB has now beaten PostgreSQL to fourth place in the overall database ranking, just behind Oracle, MySQL and Microsoft SQL Server. This article summarizes how to tune MongoDB for performance.

Have you had any performance issues with MongoDB? Here is a summary of the five steps to optimize MongoDB performance, which I hope will help.

Step 1: find slow statements

1 generally speaking, slow query statements are directly related to performance bottlenecks, so you can use MongoDB's performance analysis tool to find these slow statements:

db.setProfilingLevel(1, 100);

Step 2: analyze using explain

These slow statements are diagnosed using explain. You can also use mtools to analyze logs.

Step 3: create the index

After analysis, a new index (index) needs to be created to improve the performance of the query. Don't forget that in MondoDB you can create indexes in the background to avoid collections locks and system crashes.

Step 4: use sparse indexes to reduce space usage

If you use sparse documents and heavily use the keyword $exists, you can use sparse indexes to reduce space usage and improve query performance.

Step 5: read and write separation

If the read and write are all in the master node, from the node 1 is in the empty state, which is a waste. For a read operation like a report or a search, it is entirely possible to do this from a slave node, so set it to secondarypreferred in connection string.

A small summary

Although these methods can play a definite role, the main purpose is to buy some time for architectural improvement.

All right, that's the end of this article. I hope it will be helpful for you to learn MongoDB performance optimization.


Related articles: