In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Prepare for
Before that, let's insert 100000 pieces of data into our database. The format of the data is as follows:
{"name": "your name", "age": 22, "gender": "male", "grade": 2}
Explain
The explain method is used to view some query information of db.collecion.find (). For example:
Db.collectionName.find () .explain ()
The explain method has an optional parameter, verbose, which is a string that represents the pattern of verbose. There are three modes:
QueryPlanner: default parameter that details the plan selected by the query optimizer and lists the plans that have been rejected. For example:
Db.students.find ({grade:1}) .explain ()
ExecutionStats:MongoDB runs the query optimizer to select the winning plan, execute the plan, complete and return success, and count the execution of the victory plan described. For example:
Db.students.find ({grade:1}) .explain ("executionStats")
AllPlansExecution:MongoDB returns the statistics captured when describing the execution of the award-winning plan and statistical plan selection for other candidates.
Our goal is to record the time it takes to execute the find method, so the executionStats mode is fine.
The returned result can only focus on executionStats, as shown below:
NReturned: represents the number of documents returned under the query condition. ExecutionTimeMills: indicates the execution time, in milliseconds totalDocsExamined: represents the total number of documents in the collection.
I won't say much about other attributes here, but we'll just take executionTimeMills for recording time.
Profiling
The method mentioned above seems to be only applicable to the find method, and it is impossible to count the time spent by query methods such as aggregate queries. Here is another profiling method to record the query time.
Enable the Profiling function
There are two ways to control the switch and level of Profiling. The first is to set it directly in the startup parameters.
Add the-profile= level when you start MongoDB. You can also invoke the db.setProfilingLevel (level) command on the client side for real-time configuration. You can get the current Profile level through the db.getProfilingLevel () command.
For example:
Db.setProfilingLevel (2) db.getProfilingLevel ()
Profiling is divided into three levels:
0-not enabled. 1-record slow commands (default is > 100ms) 3-record all commands
The Profile record logs slow commands at level 1, so what is the definition of slow? As we mentioned above, it defaults to 100ms. Of course, there are default settings. There are two setting methods and levels. One is to start the configuration by adding-slowms parameters. The second is to add a second parameter when calling db.setProfilingLevel:
Db.setProfilingLevel (level, slowms) db.setProfilingLevel (1,10)
Query Profiling record
When the profiling function is enabled, the system will record the relevant command details in the system.profile collection of the current database. The query method is the same as a normal collection query.
Db.system.profile.find ()
Mills is the time-consuming record of the command.
Since we set the level to 2, all commands are logged, and now we change it to level 1, and only record records that take more than 20 milliseconds:
Db.setProfilingLevel (1,20)
Then we execute the aggregate query to see the time elapsed:
Db.students.aggregate ({$group: {_ id: "$grade", avgAge: {$avg: "$age"})
Db.system.profile.find () .pretty ()
As you can see, our aggregate query takes 70 milliseconds.
Profile partial field explanation
Op: operation type ns: collection checked commond: contents of command docsExamined: number of scanned documents nreturned: number of records returned millis: time-consuming, unit millisecond ts: command execution time responseLength: length of returned content
Here are some common query commands:
Lists Profile records whose execution time is longer than a certain limit (for example, 20ms).
Db.system.profile.find ({millis: {$gt:50}})
View the latest 3 Profile records:
Db.system.profile.find () .sort ({$natural:-1}) .limit (3)
View the slow query operations related to a collection:
Db.system.profile.find ({ns:'mydb.students'})
MongoDB query optimization
If docsExamined (the number of records scanned) is much greater than nreturned (the number of records returned), then we need to consider using indexing to optimize record location.
If the responseLength is too large, then the result set we returned is too large. Please check whether the second parameter of the find function is only written with the property name you need. (similar to not always select in MySQL)
The advice for creating indexes is: if you read very little, try not to add indexes, because the more indexes you have, the slower the write operation will be. If there is a lot of reading, it is cost-effective to create an index.
Efficiency of Profiler
The Profiling function will certainly affect efficiency, but it is not too serious, because it uses system.profile to record, and system.profile is a capped collection. This collection has some limitations and features in operation, but it is more efficient.
Summary
The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.