After learning replication, sharding, and clustering solutions in MongoDB, in this chapter, we will review the possible ways and methods to improve the response time and performance of read/write operations. Improving the performance of the database depends on several parameters, so it's difficult to pack everything in a single chapter. Nevertheless, we will discuss topics related to performance improvement one by one.
You're reading from MongoDB High Availability
To deal with poor performance in an application, we need a tool to find the problems and address the issues. In this chapter, we will focus on profiling, which is a popular method to debug and find the areas that affect performance in a program.
Note
In software engineering, profiling (program profiling and software profiling) is a form of dynamic program analysis that measures, for example, space (memory) or time, the complexity of a program, the use of particular instructions, or the frequency and duration of function calls. The most common use of profiling information is to aid program optimization. For more information, please visit http://en.wikipedia.org/wiki/Profiling_(computer_programming).
In the next sections, you will learn the basic considerations and configurations to enable and use the profiling facility in MongoDB.
MongoDB provides the explain()
function, which provides you a detailed procedure for the current operator. For instance, consider following command:
db.testcollection.find().sort({x: -1}).explain()
The preceding command will execute the find
operator and sort the records descending from the x
field. Then, the explain()
function shows an output as follows:
{ "cursor": "BasicCursor", "isMultiKey": false, "n": 100000, "nscannedObjects": 100000, "nscanned": 100000, "nscannedObjectsAllPlans": 100000, "nscannedAllPlans": 100000, "scanAndOrder": true, "indexOnly": false, "nYields": 1563, "nChunkSkips": 0, "millis": 406, "server": "mongotest:27017", "filterSet": false }
In the preceding JSON format, the n
field is an integer that reflects the number of affected records. Also, the indexOnly
field is Boolean and returns true
only when returned fields are already indexed.
Moreover, the millis
field is an integer...
In order to boost the database's performance, we can use indexes to categorize the records of a collection. Thus, the database engine can perform read/write queries faster than before.
Note
Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must scan every document in a collection to select those documents that match the query statement. These collection scans are inefficient because they require mongod
to process a larger volume of data than an index for each operation. For more information on indexes, please visit http://docs.mongodb.org/manual/core/indexes-introduction/.
If you frequently work with specific fields in a collection, we recommend that you define an index for them to improve performance. For instance, if you sort a collection based on certain fields regularly, you can make that field an indexed field so that the database engine can perform the query much faster.
To define an index for a specific field, you can use following...
In this chapter, we reviewed basic approaches to profiling, analyzed the database engine, and you learned how to boost the performance of a database. First, we introduced the profiling feature, which is a method to analyze and find slow operations in a database. Then, you learned how to enable and use profiler logs in MongoDB to find slow operations. Next, you learned other profiling functions and methods to find the in-progress operations in MongoDB or get a report from a specific query. Furthermore, we reviewed an example of using profiling for slow operations and how to find them in the system.profile
collection.
Next, you discovered how to use indexing, projection, and limiting features to limit the database output to a specific subset of data and perform operations faster than before. Also, we demonstrated an example of indexing, projection, and limiting features.
At the end of chapter, we reviewed some hardware considerations that can be used to boost the overall performance...