MongoDB的索引 简单讲解 - Go语言中文社区

MongoDB的索引 简单讲解


1.索引的查看

返回一个集合中的所有的索引的数组

> db.comment.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "articledb.comment"
	}
]

2.索引的创建

单字段索引
对userid字段建立升序索引

> db.comment.createIndex({userid:1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

#这时再查询一下,发现有两个索引了
> db.comment.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "articledb.comment"
	},
	{
		"v" : 2,
		"key" : {
			"userid" : 1
		},
		"name" : "userid_1",
		"ns" : "articledb.comment"
	}
]

复合索引
对userid和nickname同时建立符合索引,userid升序索引,nickname降序索引

> db.comment.createIndex({userid:1,nickname:-1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 2,
	"numIndexesAfter" : 3,
	"ok" : 1
}

> db.comment.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "articledb.comment"
	},
	{
		"v" : 2,
		"key" : {
			"userid" : 1
		},
		"name" : "userid_1",
		"ns" : "articledb.comment"
	},
	{
		"v" : 2,
		"key" : {
			"userid" : 1,
			"nickname" : -1
		},
		"name" : "userid_1_nickname_-1",
		"ns" : "articledb.comment"
	}
]

3.索引的移除

根据创建索引的条件来删除索引

> db.comment.dropIndex({userid:1})
{ "nIndexesWas" : 3, "ok" : 1 }
> db.comment.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "articledb.comment"
	},
	{
		"v" : 2,
		"key" : {
			"userid" : 1,
			"nickname" : -1
		},
		"name" : "userid_1_nickname_-1",
		"ns" : "articledb.comment"
	}
]

根据索引的名称来删除索引

> db.comment.dropIndex("userid_1_nickname_-1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.comment.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "articledb.comment"
	}
]

删除所有的索引

> db.comment.dropIndexes()
{
	"nIndexesWas" : 1,
	"msg" : "non-_id indexes dropped for collection",
	"ok" : 1
}

#_id不会被删除
> db.comment.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "articledb.comment"
	}
]

4.索引的使用

4.1 执行计划

> db.comment.find({userid:"1003"}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "articledb.comment",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"userid" : {
				"$eq" : "1003"
			}
		},
		"winningPlan" : {
			"stage" : "COLLSCAN",
			"filter" : {
				"userid" : {
					"$eq" : "1003"
				}
			},
			"direction" : "forward"
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "hadoop",
		"port" : 27017,
		"version" : "4.0.10",
		"gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766"
	},
	"ok" : 1
}

在compass中输入{userid:“1003”},可以看到扫描了5条数据,说明没有用上索引
在这里插入图片描述

#创建索引
> db.comment.createIndex({userid:1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

> db.comment.find({userid:"1003"}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "articledb.comment",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"userid" : {
				"$eq" : "1003"
			}
		},
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "IXSCAN",
				"keyPattern" : {
					"userid" : 1
				},
				"indexName" : "userid_1",
				"isMultiKey" : false,
				"multiKeyPaths" : {
					"userid" : [ ]
				},
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 2,
				"direction" : "forward",
				"indexBounds" : {
					"userid" : [
						"["1003", "1003"]"
					]
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "hadoop",
		"port" : 27017,
		"version" : "4.0.10",
		"gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766"
	},
	"ok" : 1
}

变成了抓取:
在这里插入图片描述
而之前的是全局扫描(collscan):
在这里插入图片描述
下面我们再在compass中执行一下:
可以看到文档的扫描,只扫描了两条数据
在这里插入图片描述
它是先通过ixscan先查询索引的集合,查询索引集合之后,在通过抓取来抓取查询来的两个,就不用了全集合扫描了
在这里插入图片描述

4.2 涵盖的查询

投影查询
Query covered by index:查询被索引覆盖了
这种情况下,就不会去文档的真实集合里去找记录了,直接从索引里返回就ok了
在这里插入图片描述
在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_45775873/article/details/109606941
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢