MongoDB Performance Optimization

Source: Internet
Author: User

indexing is the most direct means of optimizing a database. Following the index optimization guidelines, you can establish more efficient and reasonable indexes.

All columns that contain conditions in the index can be used to reject inappropriate rows in the result set by using the index-formed mask for the reference column that needs to be sorted, creating the index appropriately avoids the administrative overhead of sorting, and avoids using more than 5 columns in the index for multi-column indexes. Place the most referenced column in the query before the definition do not include columns that are frequently modified or inserted, deleted in the index (except for primary and foreign keywords) the "$" symbol cannot be the first letter of the index, "." Cannot appear anywhere in the index name.

Index management

1. Function for indexing: ensureindex ();

eg. index 1 (Ascending) on name, 1 (descending), default to Ascending. Db.person.ensureIndex ({name:1}); When the system already has a large amount of data, creating an index is time-consuming and needs to be performed in the back, only to specify background: True to. Db.user.ensureIndex ({age:1}, {background:true}); After an index is established, the same query statement compares the number of record bars for 2 scans .> Db.user.find ({name: "User 5 "}). Explain ();

2. Get the index information in the collection

Db.person.getIndexKeys ();d b.person.getindexes ();

3. Create a unique index

You only need to specify "Unique:true" in the Ensureindex command to create a unique index

Db.user.ensureIndex ({age:1}, {unique:true});

A field that creates a unique index does not allow the insertion of the same value.

4. Create a composite index

Db.user.ensureIndex ({"addr.city": 1, "Addr.state": 1}); Creates a composite index on the addr.city and addr.state columns of the user collection.

5. Deleting an index

Db.user.dropIndexes (); Delete all indexes Db.user.dropIndex ({age:1}) except the ID in the Usre table; Delete The age index from the user table

Performance optimization

Explain view execution plan

MongoDB provides a explain command to let us know how the system handles query requests, and with the explain command we can observe the system very well

How to use indexes to speed up retrieval while targeting optimized indexes.

Optimizer profile

The MongoDB database Profiler is a slow query log feature that can be used as a basis for optimizing our databases.

Turn on the profiling function, there are 2 ways to control the profiling switch box level.

Add-profile= level when starting Monggodb

The Db.setprofilinglevel (level) command is called on the client to configure in real time.

The profiler information is stored in system.profile. We can get the current profile level through the Db.getprofilinglevel () command.

There are 3 levels of profile, 0, 1, 2, respectively. Default is not turned on

0-Indicates do not turn on 1-Indicates record slow command (default = >100ms) 2-Indicates all commands are logged

Profile will record slow command at level 1 o'clock, default value is 100ms, there are 2 ways to set slow recording

1. Add the second parameter when configuring 2.db.setprofilinglevel (1, setting MS parameter) via the Slowms startup parameter.

Performance monitoring

By monitoring the performance of the database, we can better understand the working state of the database and optimize it.

Several performance monitoring tools:

1.mongosniff

This feature can be monitored from the bottom to what commands are sent to MongoDB to execute,

./mongosniff--source Net Lo

It is dynamically monitored in real time and needs to be opened by another client for command operation. It can be said that the data output to a log file,

Then you can keep a history of all database operations, which is a huge contribution to the performance analysis and security audits.

"Lo" is loopback, which means that IP packets are sent to this machine, typically using 127.0.0.1 as the loopback address, loopback is typically used to detect network configuration and communication between local programs.

2.mongostat

This tool allows you to quickly view statistics for a set of running MongoDB instances, as well as to open another client for command operation

./mongostat

3.db.serverstatus command

This command allows you to view the server running status, which is the most basic command to view the running status of the instance.

4.db.stats command

MongoDB not only can view the instance-level run state information, but also can run the "db.stats" command to view the detailed running state of a particular database and to analyze the fault more finely.

Optimization scenarios

Optimization Scenario 1: Creating an Index

Creating an index on a field of a query condition, or on a field of sort criteria, can significantly improve execution efficiency.

Db.user.ensureIndex ({name:1});

Optimization Scenario 2: Limit the number of returned result bars

Using the limit () limit to return the size of the result set can reduce the resource consumption of database server and reduce the amount of data transmitted over the network.

Db.user.find (). Sort ({age:-1}). Limit (5);

Optimization Scenario 3: Query The fields used, do not query all fields

Db.user.find ({}, {name:1}). Sort ({age:-1}). Limit (5);

Optimization Scenario 4: Using Capped collection

Capped collections higher reading and writing efficiency than normal collections

Optimization Scenario 5: Using profiling

Profiling function is sure to affect the efficiency, but not too serious, because he is using System.profile to record, System.profile is a capped collection This collection has some limitations and features in operation, but it is more efficient.


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1542480

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.