MongoDB的索引

创建索引

删除索引

创建唯一的索引

创建联合索引


创建索引:

 

目的:提升查询效率

使用实例进行测试:

先将100000条数据插入数据库中

for(i=0;i<100000;i++){
    db.t255.insert(
        {name:'test'+i,age:i}
    )
}

查询name为"test100000"数据

db.t255.find({name:"test100000"}).explain("executionStats");

结果为:

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "test.t255",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "name" : {
                                "$eq" : "test100000"
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "name" : {
                                        "$eq" : "test100000"
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 0,
                "executionTimeMillis" : 43,      #查询的时间 43ms
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 100000,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "name" : {
                                        "$eq" : "test100000"
                                }
                        },
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 43,
                        "works" : 100002,
                        "advanced" : 0,
                        "needTime" : 100001,
                        "needYield" : 0,
                        "saveState" : 781,
                        "restoreState" : 781,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 100000
                }
        },
        "serverInfo" : {
                "host" : "DESKTOP-CXTL368",
                "port" : 27017,
                "version" : "3.6.20",
                "gitVersion" : "39c200878284912f19553901a6fea4b31531a899"
        },
        "ok" : 1
}
 
此时建立name的索引,再进行查询
db.t255.ensureIndex({name:1})

结果:

#输出
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,    #创建之前有多少个索引(1为_id)
    "numIndexesAfter" : 2,     #创建之后有多少个索引
    "ok" : 1
}

此时再去查询name:test100000的时间

"executionTimeMillisEstimate" : 0,

由此可以发现创建索引可以大幅度的提高查询效率。

查询当前数据库中有几个索引

db.t255.getIndexs();
#结果
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1     #按照升序的方式
                },
                "name" : "_id_",
                "ns" : "test.t255"
        },
        {
                "v" : 2,
                "key" : {
                        "name" : 1
                },
                "name" : "name_1",
                "ns" : "test.t255"
        }
]

删除索引:

db.t255.dropIndex({name:1});

创建唯一的索引:

在默认的情况下,创建索引是允许索引值有重复值的。

db.stu.ensureIndex({name:1},{unique:true});

上述语句就可以创建唯一的索引,但是当原有数据集中,将要创建的索引的索引值不唯一,则创建索引会失败。

即:若想为name建立索引,name的值中有2个“李明”,则创建索引就会失败。

 

创建联合索引

db.stu.ensureIndex({name:1,age:1});

使用联合索引的目的:

进行数据去重,例如爬虫得的数据,想要筛选出有价值的数据,需要有多个测量指标。

 
posted @ 2021-01-21 23:26  金鳞踏雨  阅读(4)  评论(0编辑  收藏  举报  来源