cannedObjects" : 55556,
"nscanned" : 55556,
"nscannedObjectsAllPlans" : 55657,
"nscannedAllPlans" : 55657,
"scanAndOrder" : true,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 404,
"indexBounds" : {
"type" : [
[
1,
1
]
],
"sex" : [
[
1,
1
]
],
"id" : [
[
0,
1000000
]
],
"time" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
},
"server" : "node1:27017"
} 看到了没有,虽然查询时间缩短了,但是这个查询结果还是会排序结果,好,我们再把索引改改:
db.test.ensureIndex({type:1,sex:1,time:-1,id:1},{name:'index_tist'})
{
"cursor" : "BtreeCursor index_tist",
"isMultiKey" : false,
"n" : 55556,
"nscannedObjects" : 55556,
"nscanned" : 55556,
"nscannedObjectsAllPlans" : 55657,
"nscannedAllPlans" : 55657,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 168,
"indexBounds" : {
"type" : [
[
1,
1
]
],
"sex" : [
[
1,
1
]
],
"time" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
],
"id" : [
[
0,
1000000
]
]
},
"server" : "node1:27017"
} 再来看看,快到什么程度了,这个查询的速度和参数条件已经比上一个索引的快了很多,那为什么会出现这种情况呢?为什么time在id的前后会有不同的表现?这是因为通过type和sex字段过滤完之后,已经在内存中有了数据,而这些数据下一步需要怎么办?是先通过id来筛选,还是按照排序筛选呢?这里有一个知识点,在把id放在time前面时,程序首先会取复合id值,然后再把复合的数据排序,但是如果id放在排序的后面,那么程序将直接通过顺序扫描索引树的方式取出复合id范围的数据。
四,总结
1.mongodb创建索引难点在于排序和范围查询的字段位置选择
2.mongodb的复合索引的索引截取查询是顺序的,即如果(a:1,b:1,c:1},则可以是查询{a:1},{a:1,b:1},{a:1,b:1,c:1}中得任何一种都会使用该索引,其它查询情况将不会用到该索引;
3.尽量创建更少的索引以提高数据库性能
4.以上的索引优化只是生产环境的一部分,具体情况可能还要看自己的业务来定