MongoDB performance optimization and monitoring _mongodb

Source: Internet
Author: User
Tags mongodb mongodb monitoring

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB is a product between relational database and non relational database, and is the most powerful and relational database in the relational database.

First, index

MongoDB provides a variety of index support, indexed information is kept in system.indexes, and the default is always to create an index for _id, which uses the same relational database as MySQL. In fact, it can be said that the index is over the data storage system on top of the other layer of system, so the different structure of storage have the same or similar index implementation and use interfaces is not surprising.

1. Basic Index

Create an index on the field age, 1 (ascending);-1 (Descending):

 
 

_ID is an index that is automatically created when a table is created, and cannot be deleted by this index. When the system has a lot of data, creating an index is a very time-consuming activity, we can execute in the background, just specify "Backgroud:true" can be.

 
 

2. Document indexing

An index can have any type of field, or even a document:

Db.factories.insert ({name: "WWL", addr: {city: "Beijing", State: "BJ"});
Create an index
db.factories.ensureIndex ({addr:1}) on the addr column
; The following query will use the index
db.factories.find ({addr: {city: "Beijing", State: "BJ"}) that we have just established;
But the following query will not use the index because the order of the queries is different from the order in which the indexes were established

3. Combined Index

As with other database products, MongoDB also has a combined index, we will build a composite index on addr.city and addr.state. When you create a composite index, the 1 after the field is ascending, and-1 is in descending order, whether 1 or 1 is related to the time of the sorting or the specified range of queries.

Db.factories.ensureIndex ({"addr.city": 1, "Addr.state": 1});
The following query uses this index
db.factories.find ({"addr.city": "Beijing", "addr.state": "BJ"});
Db.factories.find ({"addr.city": "Beijing"});
Db.factories.find (). Sort ({"addr.city": 1, "Addr.state": 1});

4. Unique index

You can create a unique index by simply specifying "Unique:true" in the Ensureindex command. For example, insert 2 records into the table T4:

 
 

5. Forcing the use of indexes

The hint command can force an index to be used.

 
 

6. Delete Index

Delete all indexes in the T3 table
db.t3.dropIndexes ()
//delete firstname index in T4 table

Ii. Explain implementation plan

MongoDB provides a explain command to learn how the system handles query requests. With the explain command, we can see how the system uses indexes to speed up retrieval, while optimizing indexes.

Db.t5.ensureIndex ({name:1})
Db.t5.ensureIndex ({age:1})
Db.t5.find ({age:{$gt:}}, {name:1}). Explain ()
{
"cursor": "Btreecursor age_1",
"nscanned": 0,
"nscannedobjects": 0,
"n": 0,
"Millis": 0,< c9/> "Nyields": 0,
"nchunkskips": 0,
"Ismultikey": false,
"indexonly": false,
"Indexbounds": { c14/> "Age": [
[45,1.7976931348623157e+308]
]
}
} 


Field Description:

cursor: Return cursor type (basiccursor or btreecursor)
nscanned: Number of documents scanned
N: Number of documents returned
Millis: Time consuming (milliseconds)
indexbounds: Index used

Third, optimizer profile

In MySQL, slow query log is often used as our basis for optimizing the database, that in the MongoDB have similar functions? The answer is yes, that is MongoDB database Profiler.

1. Open Profiling function

There are two ways to control Profiling switches and levels, the first of which is directly set in the startup parameters. When you start MongoDB, add the –profile= level. It is also possible to configure the Db.setprofilinglevel (level) command on the client side in real time, Profiler information is stored in system.profile. We can get the current profile level through the Db.getprofilinglevel () command, similar to the following:

 
  

The level of the above profile can be 0,1,2 three values, and they mean the following:

1.0– not open.
2.1– Record Slow command (default is >100ms)
3.2– Record All commands

The profile record slows down the command at Level 1 o'clock, so what's the slow definition? Above we said that the tacit thought that the 100ms, of course, there is a default setting, there are two kinds of setting method and level, one is by adding –slowms boot parameter configuration. The second is to invoke Db.setprofilinglevel with the second argument:

Db.setprofilinglevel (level, SLOWMS)

2. Query Profiling Records

Unlike MySQL's slow query log, the MongoDB profile record is directly present in the system DB, and the record location is System.profile, so we'll just have to check the collection record to get our profile records. List profile records with execution times longer than a certain limit (5ms):

 
 

The MongoDB Shell also provides a concise command show profile, which lists profile records with more than 1ms of recent 5 execution times.

Four, the common performance optimization plan

1. Create an index

2. Limit the number of returned results

3. Only the fields used by the query

4. Using Capped Collection

5. Using the server Side Code Execution

6. Use hint to force indexes

7. Using Profiling

V. Performance monitoring Tools

1. Mongosniff

This tool can be analyzed from the bottom to monitor which commands are sent to the MongoDB to perform: Perform as root:

 
  

It then monitors all packet requests to local localhost listening to MongoDB on the default 27017 port.

2.Mongostat

This tool allows you to quickly view statistics field descriptions for a group of MongoDB instances in run:
Insert: Insert amount per second
query: Query amount per second
Update: Update amount per second
Delete: Removals per second
Locked: Lock ration
QR | QW: Client Query Queue Length (read | write)
ar | AW: Active Client Volume (read | write)
Conn: Number of connections
Time: Current time

It refreshes the status value every second, providing good readability, through which you can observe a whole performance situation.

3. Db.serverstatus

This command is one of the most common and basic commands to view the running status of an instance.

4.db.stats

Here to introduce the next MongoDB monitoring

MongoDB can monitor the data by profile and optimize it.

To see if the Profile feature is currently open by command

Db.getprofilinglevel () Returns the level rank, the value is 0|1|2, respectively means: 0 represents closes, 1 represents slow command, 2 represents all Start profile function

Db.setprofilinglevel (level); #level等级, when the value of the same level is 1, the slow command defaults to 100ms, change to Db.setprofilinglevel (LEVEL,SLOWMS) such as Db.setprofilinglevel (1,50) This changes to 50 milliseconds to view the current monitoring log through Db.system.profile.find ().
Such as:

> Db.system.profile.find ({millis:{$gt:}}) 
{"TS": Isodate ("2011-07-23t02:50:13.941z"), "info": "Query Order.order reslen:11022 nscanned:672230 \nquery: {status:1.0} nreturned:101 bytes:11006 640ms "," Millis ": 640} 

The meaning of this value is

TS: Command Execution time
Info: The contents of the command
Query: Representing queries
Order.order: A library and collection that represents a query
Reslen: The result set size returned, the byte number
nscanned: Number of scanned records
Nquery: Back is the query condition
Nreturned: Returns the number of records and when
Millis: The time spent

If you find that the time is longer, then you need to optimize.

For example, if the number of nscanned is large or close to the total number of records, the index query may not be used.
The Reslen is large and it is possible to return unnecessary fields.
Nreturned is very large, then there may be no restrictions on the query.

MONGO can view the running status of Mongod through Db.serverstatus ()


> Db.serverstatus () {"Host": "Baobao-laptop", #主机名 "version": "1.8.2", #版本号 "process": "Mongod", #进程名 "uptime" : 15549, #运行时间 "uptimeestimate": 15351, "localtime": Isodate ("2011-07-23t06:07:31.220z"), Current Time "GlobalLock": {"total  
Time ': 15548525410, #总运行时间 (NS) "Locktime": 89206633, #总的锁时间 (ns) "ratio": 0.005737305027178137, #锁比值 "Currentqueue": { "Total": 0, #当前需要执行的队列 "readers": 0, #读队列 "writers": 0# Write Queue}, "Activeclients": {"Total": 0, #当前客户端执行的链接数 "Reade Rs ": 0, #读链接数" writers ": 0# Write Link number}}," Mem ": {#内存情况" bits ": *, #32位系统" resident ": 337, #占有物理内存数" virtual ": 599,#  
Possession of virtual memory "supported": TRUE, #是否支持扩展内存 "mapped": "Connections}": {"Current": 2, #当前链接数 "available": 817# number of available links }, "Extra_info": {"note": "Fields vary by platform", "heap_usage_bytes": 159008, #堆使用情况字节 "page_faults": 907 #页面 Pretend}, "Indexcounters": {"Btree": {"accesses": 59963, #索引被访问数 "hits": 59963, #所以命中数 "misses": 0, #索引偏差数 "reset S ": 0, #复位数" Missratio " : 0# not hit Ratio}}, "Backgroundflushing": {"Flushes": 259, #刷新次数 "Total_ms": 3395, #刷新总花费时长 "Average_ms": 13.1081081 
08108109, #平均时长 "Last_ms": 1, #最后一次时长 "last_finished": Isodate ("2011-07-23t06:07:22.725z") #最后刷新时间}, "Cursors": { "Totalopen": 0, #打开游标数 "clientcursors_size": 0, #客户端游标大小 "timedout": 16# timeout}, "network": {"Bytesin": 285676177, #输入数据 (Byte) "Bytesout": 286564, #输出数据 (byte) "numrequests": 2012348# number of requests}, "Opcounters": {"Insert": 2010000, #插入操作 Number "Query": #查询操作数 "Update": 5, #更新操作数 "delete": 0, #删除操作数 "Getmore": 0, #获取更多的操作数 "command": 148# other Command operand}, "a  Sserts ": {#各个断言的数量" regular ": 0," warning ": 0," MSG ": 0," user ": 2131," rollovers ": 0}," writebacksqueued ": False, "OK": 1} db.stats () View the original state of a library > db.stats () {"DB": "Order", #库名 "Collections": 4, #集合数 "Objects": 2011622, #记录数 "avgobjsize": 111.92214441878245, #每条记录的平均值 "datasize": 225145048, #记录的总大小 "storagesize": 307323392, #预分配的 Storage Space "numextents": 21,#事件数 "Indexes": 1, #索引数 "indexsize": 74187744, #所以大小 "fileSize": 1056702464, #文件大小 "OK": 1}  

View collection for Hire

> db.order.stats () 
{ 
"ns": "Order.order", #命名空间 
"Count": 2010000, #记录数 
"size": 225039600, #大小 
"Avgobjsize": 111.96, 
"storagesize": 307186944, "numextents": A, "nindexes": 
1, 
"lastextentsize": 56089856, 
"Paddingfactor": 1, 
"flags": 1, 
"totalindexsize": 74187744, 
"indexsizes": { 
"_ Id_ ": 74187744# index is _id_ index size 
}, 
" OK ": 1 
}

Mongostat command to view live statistics in operation, representing the number of live executions per second

MongoDB also provides an opportunity to visit the HTTP monitoring page, you can access http://ip:28017 to view, this page is basically the above commands to do a comprehensive, so here is not detailed.

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.