天天看點

mongodb慢查詢

開啟 Profiling 功能

1. 直接在啟動參數裡直接進行設定

      啟動MongoDB時加上–profile=1 即可

     還可以慢查詢時間(即大于多少時間被記入慢查詢)--slowms 200

2. 可以通過db.getProfilingLevel()指令來擷取目前的Profile級别

       db.getProfilingLevel()

          0 – 不開啟

        1 – 記錄慢指令 (預設為>100ms)

        2 – 記錄所有指令

        db.setProfilingLevel( level , slowms ) 

  db.setProfilingLevel( 1 , 10 );

3.查詢慢查詢記錄

        db.system.profile.find() 

          列出執行時間長于某一限度(5ms)的 Profile 記錄:

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

4.慢查詢參數詳解

  ts-該指令在何時執行.

  millis Time-該指令執行耗時,以毫秒記.

  info-本指令的詳細資訊.

  query-表明這是一個query查詢操作.

  ntoreturn-本次查詢用戶端要求傳回的記錄數.比如, findOne()指令執行時 ntoreturn 為 1.有limit(n) 條件時ntoreturn為n.

  query-具體的查詢條件(如x>3).

  nscanned-本次查詢掃描的記錄數.

  reslen-傳回結果集的大小.

  nreturned-本次查詢實際傳回的結果集.

  update-表明這是一個update更新操作.

  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-表明update的upsert參數為true.此參數的功能是如果update的記錄不存在,則用update的條件insert一條記錄.

  moved-表明本次update是否移動了硬碟上的資料,如果新記錄比原記錄短,通常不會移動目前記錄,如果新記錄比原記錄長,那麼可能會移動記錄到其它位置,這時候會導緻相關索引的更新.磁盤操作更多,加上索引更新,會使得這樣的操作比較慢.