Optimization Analysis of MongoDB query

Source: Internet
Author: User
Tags manual mongodb mongodb query valid mongo shell


In MySQL, the slow query log is often used as our basis for optimizing queries, is there a similar function in MongoDB? The answer is yes, that is to open the profiling function. The tool collects write operations on MongoDB, cursors, database commands, etc. on the running instance, which can be opened at the database level or at the instance level. The tool writes all the collected to the System.profile collection, which is a capped collection. For more information see: http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/



Instructions for use:



1:profiling level description



0: Close, do not collect any data.



1: Collect slow query data, default is 100 milliseconds.



2: Collect all data



2: Open Profiling and settings


1: Through MONGO Shell:
#View status: Level and Time
drug: primary> db.getprofilingstatus ()
{"was": 1, "slowms":
View level
drug: primary> db.getprofilinglevel ()
#Set level
drug: primary> db.setprofilinglevel (2)
{"Was": 1, "slowms": M, "OK": 1}
#Set level and time
drug: primary> db.setprofilinglevel (1,200)
{"was": 2, "slo WMS": +, "OK": 1}

    
If you want to operate on the set below the test set, only the operation in that collection is valid, and if you need to be valid for the entire instance, you need to set up all the sets or

open the arguments when you open them:
2: Do not pass MONGO Shell:
mongod--profile=1--slowms=15 or
add 2 lines in the configuration file: Profile
= 1
slowms = 300


3: Close Profiling



# Close



Drug:primary> db.setprofilinglevel (0)



{"was": 1, "slowms": MB, "OK": 1}



4: Modify the "Slow query log" size


#Close Profiling
drug: primary> db.setprofilinglevel (0)
{"was": 0, "slowms": MB, "OK": 1}
#Remove system. ProFi Le set
drug: primary> db.system.profile.drop ()
true
#Create a new system. Profile Collection
drug: primary> Db.createcollection ("System.profile", {capped: true, size: 4000000})
{"OK": 1}
#Restart Profiling
drug: pri Mary> db.setprofilinglevel (1)
{"was": 0, "slowms": MB, "OK": 1} 


Note: To change the size of the secondary system.profile, you must stop secondary, run it as a standalone, and then perform the above steps. When finished, restart the join replica set.



Slow query (system.profile) Description:



For more information, see the following examples: http://docs.mongodb.org/manual/reference/database-profiler/





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.