Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs Menu
Docs Home
/ /

Database Profiler

경고

데이터베이스 프로파일러 MongoDB 성능을 저하시킬 수 있습니다. 데이터베이스 프로파일러 활성화하기 전에 다음 대안 중 하나를 사용하는 것이 좋습니다.

데이터베이스 프로파일러의 성능 영향 에 대해 자세히 알아보려면 프로파일러 오버헤드를 참조하세요.

The database profiler collects detailed information about Database Commands executed against a running mongod instance, including CRUD operations and administration commands.

The profiler writes data to the system.profile collection, a capped collection in each profiled database. See Database Profiler Output for an overview of the documents the profiler creates.

The profiler is off by default. You can enable it per-database or per-instance at one of several profiling levels. When enabled, profiling affects database performance and disk use. See Database Profiler Overhead.

This page documents important database profiler administration options. For additional information, see:

사용할 수 있는 프로파일링 수준은 다음과 같습니다.

0
프로파일러가 꺼져 있고 데이터를 수집하지 않습니다. 이것이 기본 설정된 프로파일러 수준입니다.
1

프로파일러는 slowms 임계값을 초과하거나 지정된 필터하다 와 일치하는 작업에 대한 데이터를 수집합니다.

필터가 설정된 경우:

  • slowms, sampleRate 옵션은 프로파일링에 사용되지 않습니다.

  • 프로파일러는 필터일치하는 작업만 캡처합니다.

2

프로파일러가 모든 작업의 데이터를 수집합니다.

2 수준으로 설정하다 하면 프로파일러가 slowmsfilter에 대해 사용자가 제공한 값을 무시합니다.

mongod 인스턴스에 대해 데이터베이스 프로파일링을 활성화할 수 있습니다.

프로파일링을 활성화하려면 다음 방법 중 하나를 사용합니다.

MongoDB creates the system.profile collection in a database after you enable profiling for that database. The profiler uses this collection to record data.

스타트업 시 mongod 인스턴스에 대한 프로파일링을 활성화하려면 operationProfiling.mode을(를) 설정 파일에서 원하는 로깅 수준으로 설정하세요.

To enable profiling for all operations on the currently connected database, run in mongosh:

db.setProfilingLevel(2)

The shell returns the previous profiling level in was and sets the new level. "ok" : 1 indicates success:

{ "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }

새 설정은 프로파일링 수준 확인 섹션에서 확인 가능합니다.

MongoDB 5.0부터 profile 명령 또는 db.setProfilingLevel() 래퍼(wrapper) 메서드를 사용하여 데이터베이스 프로파일러 level, slowms, sampleRate 또는 filter에 대해 변경한 내용은 log file에 기록됩니다.

slowms and sampleRate are global settings that affect all databases in the process.

Profiling level and filter are database-level settings when set with profile or db.setProfilingLevel(). When set as command-line or configuration file options, they affect the entire process.

기본적으로 느린 작업의 임계값은 100밀리초입니다.

느린 작업은 MongoDB가 해당 작업에 소요하는 시간인 workingMillis를 기준으로 기록됩니다. 즉 잠금 대기 및 흐름 제어와 같은 요소는 작업이 느린 작업 임계값을 초과하는지 여부에 영향을 미치지 않습니다.

To change the slow operation threshold, use one of the following:

다음 예에서는 현재 연결된 데이터베이스의 프로파일링 수준을 1로 설정하고 mongod 인스턴스의 느린 작업 임계값을 20 밀리초로 설정합니다.

db.setProfilingLevel( 1, { slowms: 20 } )

프로파일링 수준이 1이면 프로파일러가 slowms 임계값보다 느린 작업을 기록합니다.

중요

The slow operation threshold applies to all databases in a mongod instance. It is used by both the database profiler and the diagnostic log. Set it to the highest useful value to avoid performance degradation.

For mongos, use db.setProfilingLevel() to configure slowms and sampleRate. These settings affect only the diagnostic log on mongos, not the profiler. Profiling is not available on mongos. [1]

다음 예시에서는 느린 작업을 기록하기 위해 mongos 인스턴스의 느린 작업 임계값을 20으로 설정합니다.

db.setProfilingLevel( 0, { slowms: 20 } )

읽기/쓰기 작업에 대한 프로파일러 항목진단 로그 메시지(예: mongod/mongos logmessages)에는 다음이 포함됩니다.

  • planCacheShapeHash 는 동일한 플랜 캐시 쿼리 형태를 갖는 느린 쿼리를 식별하는 데 도움이 됩니다.

    MongoDB 8.0부터 기존 queryHash 필드 planCacheShapeHash라는 새 필드 에 중복됩니다. 이전 MongoDB 버전을 사용하는 경우 queryHash 필드 만 표시됩니다. 향후 MongoDB 버전에서는 더 이상 사용되지 않는 queryHash 필드 제거 될 예정이며, 대신 planCacheShapeHash 필드 사용해야 합니다.

  • planCacheKey 느린 쿼리에 대한 쿼리 계획 캐시에 관해 더 많은 인사이트를 제공합니다.

이제 복제본 세트의 세컨더리 멤버가 느린 작업 임곗값보다 오래 걸리는 oplog 항목을 기록합니다. 이러한 느린 oplog 메시지의 특성은 다음과 같습니다.

  • diagnostic log에 세컨더리 멤버에 대해 기록합니다.

  • applied op: <oplog entry> took <num>ms 텍스트와 함께 REPL 구성 요소 아래에 기록됩니다.

  • 로그 수준(시스템 또는 구성 요소 수준)에 의존하지 않습니다.

  • 프로파일링 수준에 의존하지 않습니다.

  • slowOpSampleRate의 영향을 받습니다.

프로파일러는 느린 oplog 항목을 캡처하지 않습니다.

To profile only a randomly sampled subset of slow operations, set sampleRate in one of the following ways: [2]

기본적으로 sampleRate1.0로 설정되며, 느린 작업이 모두 프로파일링된다는 의미입니다. sampleRate0에서 1 사이로 설정하면 프로파일링 수준이 1인 데이터베이스는 sampleRate에 따라 느린 작업의 무작위로 샘플링된 비율만 프로파일링합니다.

The following example sets the profiling level to 1 and sets the profiles to sample 42% of slow operations:

db.setProfilingLevel( 1, { sampleRate: 0.42 } )

수정된 샘플링 속도 값은 시스템 로그에도 적용됩니다.

For mongos, slowms and sampleRate affect only the diagnostic log, not the profiler. [1] To set the sampling rate for mongos logging:

db.setProfilingLevel( 0, { sampleRate: 0.42 } )

중요

logLevel0으로 설정하면, MongoDB는 slowOpSampleRate 의해 결정되는 속도로 느린 작업을 진단 로그에 기록합니다.

logLevel을 더 높게 설정하면 높으면 모든 작업이 지연 시간과 관계없이 진단 로그에 표시됩니다. 단, 세컨더리의 저속 oplog 항목 메시지 로깅은 예외입니다. 세컨더리 로그는 oplog 항목만 기록하며, logLevel 을 높여도 모든 oplog 항목이 기록되지는 않습니다.

[1](1, 2) 데이터베이스 프로파일링 및 샤딩을 참조하세요.

To control which operations are profiled and logged, set a filter in one of the following ways:

For mongod, filter affects both the diagnostic log and the profiler, if enabled. For mongos, filter affects the diagnostic log only.

참고

프로파일링 filter가 설정되었다면, slowmssampleRate 옵션이 진단 로그나 프로파일러에 영향을 미치지 않습니다.

The following example sets profiling level 1 with a filter that logs only query operations taking longer than 2 seconds:

db.setProfilingLevel( 1, { filter: { op: "query", millis: { $gt: 2000 } } } )

To view the profiling level, run in mongosh:

db.getProfilingStatus()

셸은 다음과 유사한 문서를 반환합니다.

{ "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }

The returned document contains:

  • was: current profiling level.

  • slowms: operation time threshold in milliseconds.

  • sampleRate: percentage of slow operations being profiled.

To disable profiling, run in mongosh:

db.setProfilingLevel(0)

참고

프로파일링을 비활성화하면 데이터베이스 성능이 향상되고 디스크 사용량이 줄어들 수 있습니다. 자세한 내용은 데이터베이스 프로파일러 오버헤드를 참조하세요.

For development and test environments, you can enable profiling for an entire mongod instance. The profiling level applies to all databases on that instance.

Pass the following options to mongod at startup:

mongod --profile 1 --slowms 15 --slowOpSampleRate 0.5

Alternatively, specify operationProfiling in the configuration file.

This sets profiling level 1, defines slow operations as those lasting longer than 15 milliseconds, and profiles 50% of slow operations. [2]

slowms and slowOpSampleRate also affect operations recorded in the diagnostic log when logLevel is 0. Both settings also configure diagnostic logging for mongos. [2]

You cannot enable profiling on a mongos instance. To enable profiling in a sharded cluster, enable it on each mongod instance in the cluster.

You can set --slowms and slowOpSampleRate on mongos to configure the diagnostic log for slow operations.

The profiler logs database operations to the system.profile collection. Query that collection to view profiling data. For example queries, see Example Profiler Data Queries. For output details, see Database Profiler Output.

You cannot perform any operation, including reads, on the system.profile collection from within a transaction.

이 섹션에서는 system.profile 컬렉션에 대한 쿼리 예시를 보여 줍니다. 쿼리 출력 세부 정보는 데이터베이스 프로파일러 출력을 참조하세요.

Return the most recent 10 log entries:

db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()

Return all operations except command operations ($cmd):

db.system.profile.find( { op: { $ne : 'command' } } ).pretty()

Return operations for a specific collection (this example uses mydb.test):

db.system.profile.find( { ns : 'mydb.test' } ).pretty()

Return operations taking longer than 5 milliseconds:

db.system.profile.find( { millis : { $gt : 5 } } ).pretty()

Return operations in a specific time range:

db.system.profile.find( {
ts : {
$gt: new ISODate("2012-12-09T03:00:00Z"),
$lt: new ISODate("2012-12-09T03:40:00Z")
}
} ).pretty()

Return operations in a time range, exclude the user field, and sort by duration:

db.system.profile.find( {
ts : {
$gt: new ISODate("2011-07-12T03:00:00Z"),
$lt: new ISODate("2011-07-12T03:40:00Z")
}
}, { user: 0 } ).sort( { millis: -1 } )

프로파일링이 활성화된 데이터베이스에서 mongoshshow profile 헬퍼는 실행 시간이 1밀리초 이상인 가장 최근 작업 5건을 표시합니다. mongosh에서 show profile 실행:

show profile

When enabled, profiling affects database performance, especially at profiling level 2 or when using a low slowms threshold with level 1. Profiling also uses disk space, because it writes to the system.profile collection and the MongoDB logfile.

경고

프로덕션 배포에서 프로파일러를 활성화하기 전에 성능과 저장소에 미치는 영향을 고려하세요.

The system.profile collection is a capped collection with a default size of 1 megabyte, which can typically store several thousand profile documents. If you need to change the size, follow the steps below.

To change the size of the system.profile collection on the primary:

  1. 프로파일링을 비활성화합니다.

  2. system.profile 컬렉션을 제거합니다.

  3. system.profile 컬렉션을 만듭니다.

  4. 프로파일링을 다시 활성화합니다.

For example, to create a new system.profile collection of 4000000 bytes (4 MB) in mongosh:

db.setProfilingLevel(0)
db.system.profile.drop()
db.createCollection( "system.profile", { capped: true, size:4000000 } )
db.setProfilingLevel(1)

To change the size of the system.profile collection on a secondary, stop the secondary, run it as a standalone, and perform the steps above. Then, restart it as a replica set member. For more information, see Perform Maintenance on Self-Managed Replica Set Members.

[2](1, 2, 3) 이제 복제본 세트의 세컨더리 멤버가 느린 작업 임계값보다 오래 걸리는 oplog 항목을 기록합니다. 이러한 느린 oplog 메시지의 특성은 다음과 같습니다.
  • diagnostic log에 세컨더리 멤버에 대해 기록합니다.
  • applied op: <oplog entry> took <num>ms 텍스트와 함께 REPL 구성 요소 아래에 기록됩니다.
  • 로그 수준(시스템 또는 구성 요소 수준)에 의존하지 않습니다.
  • 프로파일링 수준에 의존하지 않습니다.
  • slowOpSampleRate의 영향을 받습니다.
프로파일러는 느린 oplog 항목을 캡처하지 않습니다.

돌아가기

느린 쿼리 설명

이 페이지의 내용