paulwong

          My Links

          Blog Stats

          常用鏈接

          留言簿(67)

          隨筆分類(1393)

          隨筆檔案(1151)

          文章分類(7)

          文章檔案(10)

          相冊

          收藏夾(2)

          AI

          Develop

          E-BOOK

          Other

          養生

          微服務

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          60天內閱讀排行

          Finding slow queries in MongoDB

          Database Profiling

          MongoDB Profiler is a db profiling system that can help identify inefficient

          or slow queries and operations.

          Levels of profiles available are:

          Level

          Setting

          0

          Off. & No profiling

          1

          On & only includes slow operations

          2

          On & Includes all operations


          We can enable it by setting the Profile level value using the following
          command in mongo shell :

          "db.setProfilingLevel(1)"

          By default, mongod records slow queries to its log, as defined by slowOpThresholdMs.

          NOTE

          Enabling database profiler puts negative impact on MongoDB’s performance.

          It’s better to enable it for specific intervals & minimal on Production Servers.

          We can enable profiling on a mongod basis but This setting will not propagate
          across a replica set and sharded cluster.

          We can view the output in the system.profile collection in mongo shell using show profile command, or using following:

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

          Command returns operations that took longer than 200 ms. Similarly we
          can change the values as per our need.

          Enabling profile for an entire mongod instance.

          For the purpose of development in testing, we can enable database profiling/settings for an 
          entire mongod instance. The profiling level will be applied to all databases.

           

          NOTE:

          We can't enable the profiling settings on a mongos instance. To enable the profiling in

          shard clusters, we have to enable/start profiling for each mongod instance in cluster.

           

          Query for the recent 10 entries

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

           

          Collection with the slowest queries(No. Of queries)

          db.system.profile.group({key: {ns: true}, initial: {count: 0}, reduce: function(obj,prev){ prev.count++;}})

           

          Collection with the slowest queries(No. Of millis spent)

          db.system.profile.group({key: {ns: true}, initial: {millis: 0}, reduce: function(obj, prev){ prev.millis += obj.millis;}})

           

          Most recent slow query

          db.system.profile.find().sort({$natural: -1}).limit(1)

           

          Single slowest query(Right now)

          db.system.profile.find().sort({millis: -1}).limit(1)

          posted on 2020-03-27 23:35 paulwong 閱讀(310) 評論(0)  編輯  收藏 所屬分類: MONGODB

          主站蜘蛛池模板: 临朐县| 宜章县| 上饶市| 崇义县| 进贤县| 阳泉市| 宁晋县| 陆河县| 永康市| 兴安盟| 茌平县| 阜城县| 芦溪县| 砀山县| 易门县| 达日县| 平塘县| 峨山| 武定县| 高台县| 临沧市| 北流市| 西峡县| 蓬莱市| 德惠市| 句容市| 青海省| 固原市| 华安县| 科技| 团风县| 牡丹江市| 柞水县| 洮南市| 夏河县| 五寨县| 恩施市| 奉节县| 汽车| 明星| 平陆县|