Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB Database Profiling

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

MongoDB Database Profiling

MongoDB Profiler is a system that captures database execution activities and helps identify slow queries and operations.

Profiling level

The significance of the available capture levels is as follows:

Level setting

0 disable

1 enabled, only slow operations are recorded

2 enable, record all operations

View Profiling level

> db.getProfilingLevel ()

Enable Profiler

> db.setProfilingLevel (1)

The complete command is:

Db.setProfilingLevel (level,slowms)

When level is 1, the default value for slow operation is 100ms, and if slow operation is specified as 500ms:

> db.setProfilingLevel (1500)

Note:

By default, mongod records all slow queries (defined by showOpThresholdMs and defaults to 100ms) into the MongoDB log file.

Enable Profiling only at critical times, and try not to enable it in a production environment.

Enable Profiling based on a stand-alone mongod instance. This setting will not spread to other instances through replica sets or shard clusters.

View Profiler results

System.profile is used to record, while system.profile is a capped collection. In the system.profile collection of your database, call the mongo shell command show profile, or query the system.profile collection to view the output of Profiler, such as:

Db.system.profile.find ({millis: {$gt: 1000}})

You can output slow queries with a query time of more than 1 second.

Detailed explanation of Profiler information content

Ts- when the command is executed.

Millis Time- the command takes time to execute and is recorded in milliseconds.

Info- details of this command.

Query- indicates that this is a query query operation.

Ntoreturn- the number of records required to be returned by the query client. For example, the findOne () command executes with a ntoreturn of 1. 0. Ntoreturn is n when there is limit (n) condition.

Query- specific query conditions (such as x > 3).

Nscanned- the number of records scanned by this query.

Reslen- returns the size of the result set.

Nreturned- the actual result set returned by this query.

Update- indicates that this is a update update operation.

Fastmod-Indicates a fast modify operation. See Updates. These operations are normally quite fast.

Fastmodinsert-indicates a fast modify operation that performed an upsert.

Upsert- shows that the upsert parameter of update is true. The function of this parameter is to insert a record with the condition of update if the record of update does not exist.

Moved- indicates whether the update has 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. If the new record is longer than the original record, it may move the record to another location, which will lead to the update of the relevant index. More disk operations, coupled with index updates, will make such operations slower.

Insert- this is an insert insert operation.

Getmore- this is a getmore operation, getmore usually occurs in a query with a large result set, the first query returns part of the results, and the subsequent results are obtained through getmore.

Performance optimization

Although we did not enable Profiler, in a production environment, we can still see slow operations greater than 100ms when we look at the log files.

Tail-f / data/var/log/mongodb/mongod.logMon May 2502 query MyTest.Pro query: {$query: {CutePath: / ^ 122133-1456 (-\ d +) * $/, Avail.Status: {$lt: 5}, $or: [{_ id: {$lt: 3310}}, {_ id: {$gt: 8520, $lt: 8530}, {_ id: {$gt: 9720, $lt: 9730}}]} $orderby: {Avail.Status: 1, AvgRate:-1} ntoreturn:200 ntoskip:0 nscanned:18764 scanAndOrder:1 keyUpdates:0 numYields: 10 locks (micros) 217999 nreturned:200 reslen:563505 116ms

If nscanned (the number of records scanned) is much greater than nreturned (the number of records returned), then we should consider using index to optimize record location.

If the reslen 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.

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.

See:

Http://docs.mongodb.org/manual/administration/analyzing-mongodb-performance/#database-profiling

Http://docs.mongodb.org/master/MongoDB-crud-guide.pdf

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report