mongodb(2)mongodb提升

1.索引

Mongodb的大多数索引是建立在btree上,每个集合最多只能有64个索引

1)单字段索引创建

db.user.createIndex( {age: 1} ) //1表示升序,-1表示降序

db.user.createIndex( {age: 1},{"unique": true} )//唯一索引

db.person.createIndex( {age: 1} ,{spare:ture})创建稀疏索引//针对含有该字段做索引,针对不含该字段不建立索引

db.user.createIndex( {age: 1},... ,...)

参数说明:

background,Boolean,在后台建立索引,以便建立索引时不阻止其他数据库活动。默认值 false。

unique,Boolean,创建唯一索引。默认值 false。

name,String,指定索引的名称。如果未指定,MongoDB会生成一个索引字段的名称和排序顺序串联。

扫描二维码关注公众号,回复: 2909744 查看本文章

sparse,Boolean,对文档中不存在的字段数据不启用索引。默认值是 false。

v,index version,索引的版本号。

weights,document,索引权重值,数值在 1 到 99,999 之间,表示该索引相对于其他索引字段的得分权重

2)复合索引创建

db.person.createIndex( {age: 1, name: 1} )

3)删除索引

db.user.dropIndex({age:1/-1})//移除索引

db.collection.dropIndexes();//删除该集合的所有索引

4)索引常用的一些方法

db.user.reindex()//目的类似mysql碎片整理,减少空洞。

db.user.getIndexes()//查看索引

db.user.totalIndexSize()//查看索引所占的空间大小

5)explain()优化分析

db.user.find({age:{$gt:9990}}).explain("executionStats") //执行计划

备注:https://docs.mongodb.com/v3.2/reference/explain-results/#queryplanner

{
    "queryPlanner" : {
        "plannerVersion" : 1,             //计划版本
        "namespace" : "admin.user",       //库.集合
        "indexFilterSet" : false,         //是否用到索引,false表示没有
        "parsedQuery" : {               //解析查询条件,即过滤条件
            "age" : {
                "$gt" : 9990
            }
        },
        "winningPlan" : {                //自动优化后的执行计划
            "stage" : "COLLSCAN",         //扫描方式:COLLSCAN 全表扫描 ,IXSCAN 索引扫描,FETCH 根索引去检索文档,SHARD_MERGE 合并分片结果等,详情见备注

            "filter" : {                  //  过滤条件
                "age" : {
                    "$gt" : 9990
                }
            },
            "direction" : "forward"         //方向forward
        },
        "rejectedPlans" : [ ]               //拒绝的执行计划
    },
    "executionStats" : {                    //执行计划信息
        "executionSuccess" : true,          //执行成功的状态
        "nReturned" : 9,                    //返回结果集数目
        "executionTimeMillis" : 2,          //执行时间,毫秒
        "totalKeysExamined" : 0,            //索引扫描条目
        "totalDocsExamined" : 10004,        //文档扫描条目
        "executionStages" : {           
            "stage" : "COLLSCAN",           //略,同queryPlanner的winningPlan
            "filter" : {
                "age" : {
                    "$gt" : 9990
                }
            },
            "nReturned" : 9,                //返回结果集数目 
            "executionTimeMillisEstimate" : 0,     //预估的执行时间,毫秒
            "works" : 10006,                //工作单元数
            "advanced" : 9,                //返回的中间结果数
            "needTime" : 9996,      
            "needYield" : 0,
            "saveState" : 78,
            "restoreState" : 78,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",           //方向
            "docsExamined" : 10004            //文档扫描数
        }
    },
    "serverInfo" : {
        "host" : "fatale",
        "port" : 27017,
        "version" : "3.6.7-14-g7f3489f445",
        "gitVersion" : "7f3489f445318e468be4534d7e5eedced033d9a3"
    },
    "ok" : 1
}  

 备注:

stage:扫描方式可出现的参数如下

collscan,ixscan,fetch,shard_merge,sort,limit,skip,idhack,sharding_filter,count,countscan,count_scan,subpla,text,projection;

不希望看到:collscan(全表扫),sort(使用sort但是无index),skip,subpla(未用到index的$or)

希望看到的:IXSCAN 索引扫描,FETCH 根索引去检索文档

猜你喜欢

转载自www.cnblogs.com/gg128/p/9545515.html