Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb slow query log usage referenc

Shulou Source: shulou.com Published: 2022-06-01 17:13:40 10月06日 Update

Mongodb Profiling function-> slow query function

Profiling writes the collected information to the system.profile collection, which is a capped collection.

1. Locate the mongodb slow query statement method:

Enable the Profiling feature, locate the slow query log, find the statements that execute slowly, and then optimize them. The ideas are as follows:

a. Query slow query log (system.profile) to find slow query statements that exceed the defined time

For b.explain () to view the execution plan, you can refer to the following article: https://blog.51cto.com/1937519/2301699

c. Determining the causes of slow statements, such as lack of indexes, business batch problems, etc., is the same as the traditional sql optimization of rdbms.

two。 Open slow query:

2.1:Profiling level description:

A.0: turn off without collecting any data.

B.1: collect slow query data. The default is 100 milliseconds.

C.2: collect all data

2.2: database slow query configuration (note that the configuration file is written, which takes effect after restart):

a. View status: level and time

Repsetzhou:PRIMARY > db.getProfilingStatus ()

{"was": 1, "slowms": 200}

b. View level

Repsetzhou:PRIMARY > db.getProfilingLevel ()

one

c. Set level

Repsetzhou:PRIMARY > db.setProfilingLevel (2)

{"was": 1, "slowms": 200, "ok": 1}

d. Confirm the setting level:

Repsetzhou:PRIMARY > db.getProfilingLevel ()

two

e. Description: set level and time (1000 equals 1s)

Repsetzhou:PRIMARY > db.setProfilingLevel (1pime1000) # (including level, time parameters, time is in milliseconds)

{"was": 2, "slowms": 1000, "ok": 1}

2.3: enable slow query in the configuration file:

OperationProfiling:

SlowOpThresholdMs: 200

Mode: slowOp

3. Turn off Profiling:

a. Close:

Repsetzhou:PRIMARY > db.setProfilingLevel (0)

{"was": 2, "slowms": 1000, "ok": 1}

b. Confirm:

Repsetzhou:PRIMARY > db.getProfilingStatus ()

{"was": 0, "slowms": 1000}

4. Modify the size of the system.profile:

a. Turn off Profiling:

Repsetzhou:PRIMARY > db.setProfilingLevel (0)

{"was": 0, "slowms": 200, "ok": 1}

b. Delete the system.profile collection:

Repsetzhou:PRIMARY > db.system.profile.drop ()

True

c. Create a new system.profile collection-10m:

Repsetzhou:PRIMARY > db.createCollection ("system.profile", {capped: true, size:10000000})

{"ok": 1}

d. Reopen Profiling:

Repsetzhou:PRIMARY > db.setProfilingLevel (1)

{"was": 0, "slowms": 200, "ok": 1}

5.1 slow query (system.profile) analysis:

Repsetzhou:PRIMARY > db.system.profile.find ()

a. The query is greater than the specified time such as 500 milliseconds:

Db.system.profile.find ({"millis": {$gte:500}})

b. Query specified time slow query:

Db.system.profile.find (

{

Ts: {

Gt: new ISODate ("2018-10-22T03:01:45.976Z")

Lt: new ISODate ("2018-10-22T03:01:48.976Z")

}

}

)

c. Show 5 recent events:

Show profile

5.2. slow query logs are also recorded in the log file of mongodb. If system.profile has been overwritten, you can refer to the backend log of mongodb.

Tags: Query time level log a data statement b. function file configuration reference train of thought d. location business event tradition information unit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux NVidia Microsoft Huawei