Reader small image

You're reading from  MongoDB High Availability

Product typeBook
Published inJul 2014
Publisher
ISBN-139781783986729
Edition1st Edition
Tools
Right arrow
Author (1)
Afshin Mehrabani
Afshin Mehrabani
author image
Afshin Mehrabani

Afshin Mehrabani is an open source programmer. He is studying to be a computer software engineer. He started programming and web development when he was 12 years old, as well as starting with PHP. Later, he joined the Iran Technical and Vocational Training Organization. He secured the first place and received a gold medal in a competition which was conducted across the entire country in the area of web development. He became a member of the Iran National Foundation of Elite after producing a variety of new programming ideas. He was a software engineer at the Tehran Stock Exchange and is now the head of the web development team in the Yara Company. He cofounded the Usablica team in early 2012 to develop and produce usable applications. He is the author of IntroJs, WideArea, flood.js and some other open source projects. He has contributed to Socket.IO, Engine.IO, and some other open source projects. He is also interested in creating and contributing to open source applications, writing programming articles, and challenging himself with new programming technologies. He has written different articles about JavaScript, Node.js, HTML5, and MongoDB that have been published on different academic websites. Afshin has 5 years of experience in PHP, Python, C#, JavaScript, HTML5, and Node.js in many financial and stock trading projects.
Read more about Afshin Mehrabani

Right arrow

Chapter 8. Analyzing and Improving Database Performance

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.

Understanding profiling


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.

Utilizing profiling

Writing an optimized query is one of the most significant factors to maximize performance in a database engine. When you...

Introducing other analytics methods


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...

Introducing indexes


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...

Summary


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...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MongoDB High Availability
Published in: Jul 2014Publisher: ISBN-13: 9781783986729
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Afshin Mehrabani

Afshin Mehrabani is an open source programmer. He is studying to be a computer software engineer. He started programming and web development when he was 12 years old, as well as starting with PHP. Later, he joined the Iran Technical and Vocational Training Organization. He secured the first place and received a gold medal in a competition which was conducted across the entire country in the area of web development. He became a member of the Iran National Foundation of Elite after producing a variety of new programming ideas. He was a software engineer at the Tehran Stock Exchange and is now the head of the web development team in the Yara Company. He cofounded the Usablica team in early 2012 to develop and produce usable applications. He is the author of IntroJs, WideArea, flood.js and some other open source projects. He has contributed to Socket.IO, Engine.IO, and some other open source projects. He is also interested in creating and contributing to open source applications, writing programming articles, and challenging himself with new programming technologies. He has written different articles about JavaScript, Node.js, HTML5, and MongoDB that have been published on different academic websites. Afshin has 5 years of experience in PHP, Python, C#, JavaScript, HTML5, and Node.js in many financial and stock trading projects.
Read more about Afshin Mehrabani