MongoDB Slow Query record

Source: Internet
Author: User
Tags mongodb query mongodb update mysql slow query log mongo shell

MongoDB slow query record   in MySQL, the slow query log is often used as the basis for us to optimize the database, is there a similar function in MongoDB? The answer is yes, that's MONGO database. Profiler. Not only, but there are some more detailed information than the MySQL slow Query log.  It is the subject of our passage.  There are two ways to open the Profiling function to control the Profiling switch and level, the first is directly in the startup parameters directly set.  When you start MongoDB, add the –profile= level. You can also invoke the Db.setprofilinglevel (level) command on the client to configure it in real time.  The current profile level can be obtained through the db.getprofilinglevel () command. > Db.setprofilinglevel (2);  {"was": 0, "OK": 1}  > Db.getprofilinglevel () the level of italic above can take 0,1,2 three values, they mean The following: 0– does not turn on the/-Record slow command (default is >100ms) – Record all command Profile records at Level 1 o'clock will record slow command, then what is this slow definition? Above we say that the default is 100ms, of course, there are settings, its setting method and level One There are two kinds of samples, one is configured by adding the –slowms boot parameter.  The second is to call Db.setprofilinglevel with a second argument: Db.setprofilinglevel (level, slowms)   Db.setprofilinglevel (1, 10); Query Profiling record and MySQL slow query log, Mongo profile record is directly present in the system DB, record location system.profile, so, we just query this collection record can get to our  Profile Records. > Db.system.profile.find ()   {"TS": "Thu Jan 15:19:32 GMT-0500 (EST)", "info": "Query test. $cmd Ntoretu Rn:1 reslen:66 nscanned:0&nbsp Query: {profile:2} nreturned:1 bytes:50 "," Millis ": 0}  db.system.profile.find ({info:/test.foo/})   { "TS": "Thu Jan 15:19:40 GMT-0500 (EST)", "info": "Insert Test.foo", "Millis": 0}  {"ts": "Thu Jan 29 2 009 15:19:42 GMT-0500 (EST), "info": "Insert Test.foo", "Millis": 0}  {"ts": "Thu Jan 15:19:45 GMT-050 0 (EST) "," info ":" Query Test.foo ntoreturn:0 reslen:102 nscanned:2  query: {} nreturned:2 bytes:86 "," Millis ": 0}  {"TS": "Thu Jan 15:21:17 GMT-0500 (EST)", "info": "Query Test.foo ntoreturn:0 reslen:36 NSCANNED:2&NB  Sp Query: {$not: {x:2}} nreturned:0 bytes:20 "," Millis ": 0}  {" ts ":" Thu Jan 15:21:27 GMT-0500 (EST) ", "Info": "Query Test.foo ntoreturn:0 exception bytes:53", "Millis": 88} lists profile records that have been executed longer than a certain limit (5ms): > Db.system . Profile.find ({millis: {$gt: 5}})   {"TS": "Thu Jan 15:21:27 GMT-0500 (EST)", "info": "Query test. Foo NtoreturN:0 exception bytes:53 "," Millis ": 88} View the latest profile record: Db.system.profile.find (). Sort ({$natural: -1}) Mongo Shell also provides  A more concise command show profile, which lists the last 5 execution times over 1ms of profile records.  Profile information Details: ts-when the command is executed.  Millis time-the command execution takes time, in milliseconds.  info-This command for more information.  Query-indicates that this is a query operation.  ntoreturn-the number of records returned by this query client. For example, the FindOne () command executes with a ntoreturn of 1. Ntoreturn is n when there is a limit (n) condition.  query-specific query conditions (such as x>3).  nscanned-the number of records scanned for this query.  reslen-returns the size of the result set.  nreturned-The result set actually returned by this query.  Update-indicates that this is an update operation. Fastmod-indicates a fast modify operation. See Updates.  These operations is normally quite fast.  Fastmodinsert–indicates A fast modify operation that performed an upsert. Upsert-indicates that the Upsert parameter for update is true. The function of this parameter is to insert a record with the update condition if the record for the update does not exist. moved-Indicates whether the update moved the data on the hard disk,  If the new record is shorter than the original record, it usually does not move the current record, and if the new record is longer than the original record, it is possible to move the record to a different location, which results in an update of the relevant index. More disk operations, plus index updates, can make such operations slower.  Insert-This is an insert insertion operation.  Getmore-This is a getmore operation, getmore usually occurs when the result set is larger than the query, the first query returns partial results, and subsequent results are obtained through Getmore. MongoDB Query Optimization If nscanned (number of scanned records) is much larger than nreturned (returnThe number of records to be returned), then we should consider Gazzo to optimize the record positioning. Reslen if it is too large, then the result set that we return is too large, please check if the second parameter of the Find function writes only the property name you need. (Similar to not always select * in MySQL) the recommendation for creating an index is to try not to add an index if it is rarely read, because the more indexes, the slower the write operation. If you read a large amount, then it is better to create an index.  (like an RDBMS, seemingly crap-_-!!) MongoDB Update Optimization If the Write query volume or update volume is too large, it will be advantageous to add more indexes. and ~ ~ ~ ~ (omitting n words, similar to RDBMS) use fast Modify operations when possible (and usually with these, an index).  See Updates. The efficiency of the Profiler Profiling function is sure to affect efficiency, but not too serious, because he is using 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.   http://www.2cto.com/database/201309/245233.html
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.