1、聚合函数的查询计划
db.runCommand({explain:{ aggregate: "assign", pipeline: [
{ $match: { yxbz: "Y", isSigner: { $ne: "Y" }, lrrq: { $lt: new Date(1570838400000) }, nsrmc: /^.*飞起.*$/i, dsljgDm: "13501111200" } },
{ $group: { _id: "$shhxxid", root: { $first: "$$ROOT" } } },
{ $sort: { "root.lrrq": -1 } }, { $limit: 1000 } ],
cursor: {},
allowDiskUse: true },verbosity: "executionStats"});
有的可能因为版本太低不支持以上用法,可尝试这种方式
db.assign.explain("executionStats").aggregate([
{ $match: { yxbz: "Y", isSigner: { $ne: "Y" }, lrrq: { $lt: new Date(1570838400000) }, rmc: /^.*飞起.*$/i, dsljgDm: "13501111200" } },
{ $group: { _id: "$shhxxid", root: { $first: "$$ROOT" } } },
{ $sort: { "root.lrrq": -1 } },
{ $limit: 1000 }]);
2、查询形状查询计划缓存
db.getCollectionNames().forEach(function(e){
//print("--------------------"+e+"--------------------");
var shapes = db.getCollection(e).getPlanCache().listQueryShapes();
if(shapes.length>0)
collQueryShapeStats[e+"_queryShapeNum"]=shapes.length;
shapes.forEach(function(shape){
print("==================shape-"+e+"==================");
printjson(shape);
print("==================getPlansByQuery-"+e+"-start==================");
printjson(db.getCollection(e).getPlanCache().getPlansByQuery(shape));
print("==================getPlansByQuery-"+e+"-end==================");
});
});
var e = "assign";
var shapes = db.getCollection(e).getPlanCache().listQueryShapes();
if(shapes.length>0)
collQueryShapeStats[e+"_queryShapeNum"]=shapes.length;
shapes.forEach(function(shape){
print("==================shape-"+e+"==================");
printjson(shape);
print("==================getPlansByQuery-"+e+"-start==================");
printjson(db.getCollection(e).getPlanCache().getPlansByQuery(shape));
print("==================getPlansByQuery-"+e+"-end==================");
});
3、获取各collection占用磁盘大小
var collectionNames= db.getCollectionNames();
var sum=0;
for (var i = 0; i < collectionNames.length; i++) {
var coll = db.getCollection(collectionNames[i]);
var stats = coll.stats(1024 * 1024);
print(stats.ns, stats.storageSize+"MB");
sum += stats.storageSize;
}
print(sum+"MB");
4、runCommand指定索引查询
db.runCommand({
"find" : "studentVO",
"filter" : {
"sno" : "3108008288",
"rqq" : ISODate("2012-09-30T16:00:00Z"),
"ssqz" : ISODate("2018-12-30T16:00:00Z"),
"yxbz" : "Y"
},
"sort" : {
"lrrq" : -1
},
"limit" : 1,
"hint":"sno_1_lrrq_-1"
});
5、runCommand explain 指定索引
db.runCommand({explain:{
"find" : "studentVO",
"filter" : {
"sno" : "3108008288",
"rqq" : ISODate("2012-09-30T16:00:00Z"),
"ssqz" : ISODate("2018-12-30T16:00:00Z"),
"yxbz" : "Y"
},
"sort" : {
"lrrq" : -1
},
"limit" : 1,
"hint":"sno_1_lrrq_-1"
},verbosity:"executionStats"});