MongoDB日常使用记录

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"});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值