数据库 \ MongoDB \ MongoDB Profiler

MongoDB Profiler

总点击254
简介:一概念 1、简介 Profiler是MongoDB的一个工具,用来记录分析数据库运行时候的慢查询。

一 概念

1、简介

Profiler是MongoDB的一个工具,用来记录分析数据库运行时候的慢查询。

2、Profiling Levels

profiler有3个级别


0:关闭


1:只记录操作时间高于阀值的慢查询,阀值默认为100ms


2:记录所有的操作


默认情况下profiler是关闭的,

二 操作

1、开启profiling

1)开启某一个数据库的profiling功能


db.setProfilingLevel(1|2);


2)开启该mongo实例上所有数据库的profiling


在启动的时候使用如下命令


mongod –profile 1 –slowms 15


3)指定慢查询的阀值


改变慢查询的阀值会在整个mongo实例上生效


db.setProfilingLevel(1,20)


注:如果mongoDB有分片,不能再路由服务器上开启此功能,必须在所有的分片上依次开启。

2、关闭profiling

db.setProfilingLevel(0);


当将profiling的级别设置为0时即可关闭profiling功能

3、查看profiling状态

1)字段说明


was:当前profiling级别


slowms:当前profiling阀值


2)查看profiling状态


db.getProfilingStatus()


3)只查看profiling级别


db.getProfilingLevel()

4、改变system.profile的大小

system.profile是一个Capped集合,它的默认固定大小为1Mb,可以手动更改它的大小。


1)关闭profiling


2)删除system.profile集合


3)创建新的system.profile集合并指定其大小


4)重启profiling功能

db.setProfilingLevel(0)

db.system.profile.drop()

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

db.setProfilingLevel(1)

注:当需要修改副本集的system.profile的大小时,需要先停止副本集使其单独运行,在修改完之后再重启。

5、查询数据

1)根据操作时间查询


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


2)查询操作时间大于5ms的操作


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


3)查询最近10条慢操作记录


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

三 使用

1、Profiler 的效率

开始profiling会对mongodb速度有一定影响,但是不是很严重。Profile的数据保存在system.profile集合中,而这个collection是capped collection,此类集合有固定的大小,所有里面通常保存最近的一些操作记录。

2、优化

1)当nscanned的大小远远大于nreturned大小的时候,说明数据库是从大量的documents中查找我们想要的documents,此时需要对查询条件建立索引。


2)当responseLength的值很大的时候,表示mongodb给client返回了大量的数据,这时候在需要查询时设置该查询需要的字段。

四 输出

1、profiling输出视图

{

"op" : "query",

"ns" : "test.c",

"query" : {

"find" : "c",

"filter" : {

"a" : 1

}

},

"keysExamined" : 2,

"docsExamined" : 2,

"cursorExhausted" : true,

"keysInserted" : 0,

"keysDeleted" : 0,

"writeConflicts" : 0,

"numYield" : 0,

"locks" : {

"Global" : {

"acquireCount" : {

"r" : NumberLong(2)

}

},

"Database" : {

"acquireCount" : {

"r" : NumberLong(1)

}

},

"Collection" : {

"acquireCount" : {

"r" : NumberLong(1)

}

}

},

"nreturned" : 2,

"responseLength" : 108,

"millis" : 0,

"execStats" : {

"stage" : "FETCH",

"nReturned" : 2,

"executionTimeMillisEstimate" : 0,

"works" : 3,

"advanced" : 2,

"needTime" : 0,

"needYield" : 0,

"saveState" : 0,

"restoreState" : 0,

"isEOF" : 1,

"invalidates" : 0,

"alreadyHasObj" : 0,

"inputStage" : {

"stage" : "IXSCAN",

"keyPattern" : {

"a" : 1

},

"indexName" : "a_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"a" : [

"[1.0,1.0]"

]

},

"dupsTested" : 0,

"dupsDropped" : 0,

"seenInvalidated" : 0

}

},

"ts" : ISODate("2015-09-03T15:26:14.948Z"),

"client" : "127.0.0.1",

"appName" : "MongoDB Shell",

"allUsers" : [ ],

"user" : ""

}

2、输出详解

1)system.profile.op:


操作的类型,有command、count、distinct、geoNear、getMore、group、insert、mapReduce、query、remove、update。


2)system.profile.ns:


命名空间,该操作所在的数据库及集合


3)system.profile.query

4)system.profile.command:


该操作的执行语句,大于50Kb的部分以省略号表示


5)system.profile.updateobj


update操作中需要被更新的对象


6)system.profile.cursorid


query和getmore操作中游标的Id


7)system.profile.keysExamined


Changed in version 3.2.0:Renamed from system.profile.nscanned.


索引被检索的数量,


8)system.profile.docsExamined


Changed in version 3.2.0: Renamed from system.profile.nscannedObjects.


找出结果所扫描的文档数量


9)system.profile.nmoved


该字段只有在使用MMAPv1存储引擎时会出现


10)system.profile.hasSortStage


Changed in version 3.2.0: Renamed from system.profile.scanAndOrder


hasSortStage是一个布尔值,是指是否可以使用索引的顺序来返回查询的结果,只有当其为true的时候才会展现


11)system.profile.ndeleted


本次操作删除的文档数量


12)system.profile.ninserted


本次操作新增的文档数


13)system.profile.nMatched


update操作中被查询条件匹配的文档数


14)system.profile.nModified


update操作中被修改的文档数


15)system.profile.upsert


update操作中标识upsert选项的一个Boolean值,当true时该字段会显示


16)system.profile.keysInserted

17)system.profile.writeConflicts

18)system.profile.numYield

19)system.profile.locks:


该操作持有锁的信息


a.Lock Type:


Global Represents global lock.


MMAPV1Journal Represents MMAPv1 storage engine specific lock to synchronize journal writes; for non-MMAPv1 storage engines,the mode for MMAPV1Journal is empty.


Database:数据库锁


Collection:集合锁


Metadata Represents metadata lock.


oplog Represents lock on the oplog.


b.Lock Mode:


R:共享锁


W:排他锁


r Represents Intent Shared (IS) lock.


w Represents Intent Exclusive (IX) lock.


子文档


1)acquireCount:

2)acquireWaitCount:

3)timeAcquiringMicros:

4)deadlockCount

20)system.profile.nreturned


该操作返回的文档个数


21)system.profile.responseLength


结果文档的大小,单位bytes


22)system.profile.millis


该操作所耗时间大小


23)system.profile.execStats


query操作的执行计划,如果是别的操作,该字段为空文档


子文档


stage:


inputStages:


24)system.profile.ts


该操作执行的时间


25)system.profile.client


该操作的客户端地址或IP


26)system.profile.appName


New in version 3.4


运行此操作的客户端应用的标识


27)system.profile.allUsers


认证用户的集合


28)system.profile.user


运行此操作的认证用户,如果该操作不是认证用户操纵,该字段为空字符串

参考链接:https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler/

0人推荐

推荐文章

热门标签

意见反馈 常见问题 官方微信 返回顶部