본문 바로가기
Programming/Mongo

[MongoDB] database profiler 설정 (MongoDB slow query 측정)

by guru_k 2023. 2. 3.
728x90
반응형

MongoDB에서 사용되는 CPU 가 Peak 를 치며 장애가 발생한적이 있다면 혹은 API가 생각했던것보다 오래걸리고 Tracing이 없는 상태에서 그 원인이 MongoDB로 추정된다면 database profiler를 통해 MongoDB의 slow query를 검사하여 원인을 파악해볼 수 있다.

MongoDB의 Profiler 설정 방법은 매우 간단하다. 아래 명령어를 이용하여 Profiling Level을 설정해주면 된다.

$ db.setProfilingLevel()

 

이때 Profiling Level에는 아래와 같이 3가지 단계가 있다.

Level Description
0 Profiler를 사용하지 않으며 어떠한 데이터도 수집되지 않는다.
Default 설정.
1 slowms에 설정된 시간보다 Query가 오래 걸릴경우 해당 Query를 slow query라고 판단하여 profiler는 데이터를 수집한다.
이때 slowmssampleRate를 통해 filter값을 설정할 수 있다.
2 Profiler가 모든 명령에 대한 데이터를 수집한다.

 

Profilinig Level을 2로 설정하는 것은 모든 데이터를 수집하기 때문에 database의 performance와 disk 사용량에 영향을 미칠 수 있기 때문에 사용할 때 주의를 해야 한다.

그래서 Profiling Level을 1로 설정하고 slowms 를 설정하여 특정 시간 이상 걸린 slowquery를 찾아서 해당 query를 개선시키는 편이 좋다.

Profiler Level 설정하기

예1) 100ms 이상 걸리는 slowquery만 수집되도록 Profiling Level 설정하기

$ db.setProfilingLevel(1, { slowms: 100 })

// was: 이전 profiling level
// slowms: 설정한 slowms 
// sampleRate: 설정한 sampling rate
// ok: 설정 여부
{ "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }

예2) 1s 이상 걸리는 slowquery만 수집하도록 설정하고 sampling rate는 0.5로 Profiling Level 설정하기

$ db.setProfilingLevel(1, { slowms: 1000, sampleRate: 0.5 })

// was: 이전 profiling level
// slowms: 설정한 slowms 
// sampleRate: 설정한 sampling rate
// ok: 설정 여부
{ "was" : 0, "slowms" : 1000, "sampleRate" : 0.5, "ok" : 1 }

 

이외에도 특정 operation 에만 필터를 걸어서 profiler를 설정 할 수 있다.

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

 

Profiler Level 확인하기

$ db.getProfilingStatus()

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

 

Profiler Disable

$ db.setProfilingLevel(0)

 

수집된 Slowquery 확인

특정 조건에 부합된 slowquery는 설정을 진행한 database아래 system.profile 에 저장되어진다.

예1) 5ms 이상 걸린 slowquery 찾기

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

 

자세한 내용은 아래 MongoDB 공식 문서를 참고

https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler/

 

Database Profiler — MongoDB Manual

Docs Home → MongoDB Manual The database profiler collects detailed information about Database Commands executed against a running mongod instance. This includes CRUD operations as well as configuration and administration commands. The profiler writes all

www.mongodb.com

 

728x90
반응형

'Programming > Mongo' 카테고리의 다른 글

Mongo 기간별 (날짜별) 조회  (0) 2021.10.13
MongoDB find query by key  (0) 2017.03.02

댓글