MongoDB日常使用記錄

1、聚合函數的查詢計劃

db.runCommand({explain:{ aggregate: "ysqSxslSxfp", pipeline: [ 
{ $match: { yxbz: "Y", isAssigner: { $ne: "Y" }, lrrq: { $lt: new Date(1570838400000) }, nsrmc: /^.*藍坤.*$/i, dslswjgDm: "13501111200" } }, 
{ $group: { _id: "$ysqxxid", root: { $first: "$$ROOT" } } }, 
{ $sort: { "root.lrrq": -1 } }, { $limit: 1000 } ],
 cursor: {}, 
 allowDiskUse: true },verbosity: "executionStats"});

 

有的可能因爲版本太低不支持以上用法,可嘗試這種方式

db.ysqSxslSxfp.explain("executionStats").aggregate([
{ $match: { yxbz: "Y", isAssigner: { $ne: "Y" }, lrrq: { $lt: new Date(1570838400000) }, nsrmc: /^.*藍坤.*$/i, dslswjgDm: "13501111200" } }, 
{ $group: { _id: "$ysqxxid", 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 = "ysqSxslSxfp";
         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");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章