mongodb之进阶
常用命令:
1.查看数据库空间大小 db.stats(); 默认是bytes单位 { "db" : "xxx", //当前数据库 "collections" : 27, //当前数据库多少表 "objects" : 18738550, //当前数据库所有表多少条数据 "avgObjSize" : 1153.54876188392, //每条数据的平均大小 "dataSize" : 21615831152.0, //所有数据的总大小 "storageSize" : 23223312272.0, //所有数据占的磁盘大小 "numExtents" : 121, "indexes" : 26, //索引数 "indexSize" : 821082976, //索引大小 "fileSize" : 25691160576.0, //预分配给数据库的文件大小 "nsSizeMB" : 16, "dataFileVersion" : { "major" : 4, "minor" : 5 }, "extentFreeList" : { "num" : 1, "totalSize" : 65536 }, "ok" : 1.0 } 可以传递参数: db.stats(1024) 得到的是kb单位 db.stats(1073741824); 得到的是G单位
1.profile
在使用mongo的时候, 会发现查询较为缓慢, 这个时候可以profile来监控数据并进行优化
查看是否开启profile功能 db.getProfilingLevel() 返回level等级, 0-关闭 1-记录慢命令 2-全部 开启profile: db.setProfilingLevel(1|2|0); 设置level等于1的shihou,慢命令默认是100ms 可以进行更改: db.setProfilingLevel(1, 50); 改为50毫秒 查看日志: db.system.profile.find(); /* 1 */ { "op" : "query", "ns" : "config.system.profile", "command" : { "find" : "system.profile", "filter" : {}, "$readPreference" : { "mode" : "secondaryPreferred" }, "$db" : "config" }, "keysExamined" : 0, "docsExamined" : 0, "cursorExhausted" : true, "numYield" : 0, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(2) } }, "Database" : { "acquireCount" : { "r" : NumberLong(1) } }, "Collection" : { "acquireCount" : { "r" : NumberLong(1) } } }, "nreturned" : 0, "responseLength" : 94, "protocol" : "op_command", "millis" : 32, "planSummary" : "COLLSCAN", "execStats" : { "stage" : "COLLSCAN", "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 0, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 0 }, "ts" : ISODate("2019-01-18T03:10:06.731Z"), "client" : "192.168.0.72", "appName" : "MongoDB Shell", "allUsers" : [ { "user" : "root", "db" : "admin" } ], "user" : "root@admin" }