Mongodb query optimization (slow query Profiling)

Turn on slow query Profiling

Profiling Level Description

0: Off, does not collect any data. 
1: Slow query data collection, the default is 100 milliseconds. 
2: collect all the data

1, open Profiling by modifying the configuration file

  Modifying the boot mongo.conf, insert the following code

# Enable the slow query, record 200 milliseconds 
Profile = 1 
slowms = 200

2, after the start mongodb services to be hit by a temporary mongoshell open, just close the mongodb service will not start the next time you turn, had another meeting

(1), in mongodb privileged situation, the command log in, if you can not do not have permission to write the contents back --username

 

mongo --host 127.0.0.1:27017 --username your username --password your password --authenticationDatabase admin

 

        (2), jump to the monitor to open the database query slow

use test

        (3), provided Profiling

Copy the code
1: the shell by Mongo: 
# Check Status: the level and time of 
Drug: a PRIMARY> db.getProfilingStatus ()    
{ "WAS": 1, "slowms": 100} 
# Check level 
Drug: a PRIMARY> db.getProfilingLevel ()     
1 
# Set level 
Drug: a PRIMARY> db.setProfilingLevel (2) 
{ "WAS":. 1, "slowms": 100, "OK":. 1} 
# time and set the level of 
Drug: a PRIMARY> db.setProfilingLevel (1,200) 
{ "WAS": 2, "slowms": 100, "ok": 1}
Copy the code

(4), modify the "slow query log" in size

Copy the code
# Close the Profiling 
Drug: a PRIMARY> db.setProfilingLevel (0) 
{ "WAS": 0, "slowms": 200 is, "OK":. 1} 
# remove system.profile collection 
drug: PRIMARY> db.system.profile.drop ( ) 
to true 
# create a new set of system.profile 
Drug: PRIMARY> db.createCollection ( "system.profile", {capped: to true, size: 4000000}) 
{ "the ok": 1} 
# reopened Profiling 
Drug: PRIMARY> db.setProfilingLevel (. 1) 
{ "WAS": 0, "slowms": 200 is, "OK":}. 1
Copy the code

Note: To change the size of Secondary system.profile, you must stop Secondary, run it as an independent, and then perform the steps above. Upon completion, restart join replica set.

Slow query ( system.profile) Description:

By following examples illustrate, for more information, see: http://docs.mongodb.org/manual/reference/database-profiler/

1: Parameter Meaning

Copy the code
Copy the code
drug:PRIMARY> db.system.profile.find().pretty()
{
    "op" : "query",    #操作类型,有insert、query、update、remove、getmore、command   
    "ns" : "mc.user",  #操作的集合
    "query" : {        #查询语句
        "mp_id" : 5,
        "is_fans" : 1,
        "latestTime" : {
            "$ne" : 0
        },
        "latestMsgId" : {
            "$gt" : 0
        },
        "$where" : "new Date(this.latestNormalTime)>new Date(this.replyTime)"
    },
    "cursorid" : NumberLong("1475423943124458998"),
    "ntoreturn": 0, # return the number of records for example, profile command returns a document (a result file), so ntoreturn value will 1.limit (5) command returns five documents, therefore ntoreturn value is 5. If ntoreturn. is 0, the command does not specify a number of documents returned, because this would be a simple find () command does not specify a limit. 
    "ntoskip": 0, #skip () method of the specified number of hops 
    " nscanned ": 304, # number of scan 
    " keyUpdates ": 0, # index number of updates, changes in a bond index with a small performance overhead, because the database must delete the old key, and insert a new key to B - tree index 
    " numYield ": 0, # the query so that the number of times a lock for other queries 
    "lockStats": {# lock information, R: global read lock; W: global write lock; r: read lock specific database; w: write lock on a particular database 
        " timeLockedMicros ": {# lock 
            " R & lt ": NumberLong (19467), 
            " W ": NumberLong (0) 
        }, 
        "timeAcquiringMicros ": {# lock waiting 
            " r ": NumberLong (7) ,
            "W": NumberLong (. 9)
        } 
    }, 
    " Nreturned ": 101, the number returned # 
    " responseLength ": 74659, # response byte length 
    " of millis ":. 19, # consumed time (ms) 
    " TS ": ISODate ( "2014-02-25T02: 13: 54.899Z "), the time statements executed # 
    " client ":" 127.0.0.1 ", # ip or the host link 
    " allUsers ": [],      
    " user ":" "user # 
}
Copy the code
Copy the code

 In addition to the above are:

Copy the code
Copy the code
scanAndOrder : 
scanAndOrder is a Boolean value that is True when a query file can not be used in the sequential ordering of the index returns the result: MongoDB in which the received file must be sorted from a file of a cursor. 
If scanAndOrder is False, MongoDB indexable return results sort order of use of these files. Namely: True: document sorting, False: use the index. 

moved 
update move one or more files on the disk to a new location. The update indicates whether to move the data on the hard disk, if the new record is shorter than the original recording, usually does not move the current record if the new record is longer than the original recording, the recording may move to another location, this time will result in the relevant index . updates more disk operations, plus an index 
update, would make such operations more slowly. nmoved: file operations on the disk. nupdated: The number of updated documents
Copy the code
Copy the code

getmore is a getmore operation, getmore typically occurs when a query result set is relatively large, the first query returns a partial result, the result is to obtain a subsequent pass of getmore.

If nscanned (the number of records scanned) is much larger than nreturned (returns the result of the number of records), then, to consider the Caucasus attracted by optimizing the positioning of the recording. responseLength  if too large, indicating the returned result set is too large, then only need to look at whether the necessary fields.

2: Query for everyday use

Copy the code
Copy the code
# Returns last 10 records 
db.system.profile.find () limit (10) .sort ({TS: -1}).. Pretty () 

# Returns all operations, command types other 
db.system.profile .find ({OP: NE {$: 'Command'}}) .pretty () 

# returns a particular set 
db.system.profile.find ({NS: 'mydb.test'}) .pretty () 

# returns greater than 5 ms slow operation 
db.system.profile.find ({of millis: {$ gt:}}. 5) .pretty () 

# returns information from within a specific time range 
db.system.profile.find ( 
                       { 
                        TS: { 
                              $ gt: new new ISODate ( "2012-12-09T03: 00: 00Z"), 
                              $ lt: new new ISODate ( "2012-12-09T03: 40: 00Z") 
                             } 
                       } 
                      ) .pretty ()

# Specific time, limit user, sorted according to the time-consuming 
db.system.profile.find ( 
                       { 
                         TS: { 
                               $ gt: new new ISODate ( "2011-07-12T03: 00: 00Z"), 
                               $ lt: new new ISODate ( "2011 -07-12T03: 40: 00Z ") 
                              } 
                       }, 
                       {User: 0} 
                      ) .sort ({of millis: -1})
Copy the code
forward from:
https://www.cnblogs.com/zhang-ke/p/7800080.html

Guess you like

Origin www.cnblogs.com/xibuhaohao/p/11301461.html