来源:http://francs3.blog.163.com/blog/static/40576727201210202322706/

关于索引的创建在之前的 创建索引 blog 中已有描述,
接下来看看 MongoDB 的执行计划,通过使用 explain()  方法可以很容易的查看执行计划。


--1 查看索引

 > db.test_4.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "skytf.test_4",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "skyid" : 1
                },
                "ns" : "skytf.test_4",
                "name" : "idx_test_4_skyid",
                "background" : true
        }
]
   


--2 查看执行计划

 db.test_4.find({skyid:1}).explain();
{
        "cursor" : "BtreeCursor idx_test_4_skyid",
        "isMultiKey" : false,
        "n" : 1,
        "nscannedObjects" : 1,
        "nscanned" : 1,
        "nscannedObjectsAllPlans" : 1,
        "nscannedAllPlans" : 1,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 9,
        "indexBounds" : {
                "skyid" : [
                        [
                                1,
                                1
                        ]
                ]
        },
        "server" : "redhatB.example.com:27017"
}
   

  备注: 上面为 mongodb 的执行计划,部分参数意思为以下:
        cursor:   值为 BasicCursor  或 BtreeCursor,后者表示此查询使用了索引
        nscanned: 扫描的索引项
        n:         返回的文档数,即返回的行
        millis:   完成此查询所需的时间,单位为毫秒
        indexBounds: 如果不为空,表示此查询使用的索引项
       
       
   MongoDB 也能强制使用索引,通过使用 hint() ,感觉非常强大!


--3 删除老索引

 > db.test_4.dropIndexes('idx_test_4_skyid');
{
        "nIndexesWas" : 2,
        "msg" : "non-_id indexes dropped for collection",
        "ok" : 1
}
   


--4 新建索引

 db.test_4.ensureIndex({skyid:1,name:1},{name:"idx_test_4_skyid_name",background:true}); 
   


--5 查看索引

 db.test_4.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "skytf.test_4",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "skyid" : 1,
                        "name" : 1
                },
                "ns" : "skytf.test_4",
                "name" : "idx_test_4_skyid_name",
                "background" : true
        }
]
   
   备注:索引 "idx_test_4_skyid_name" 已创建。


--6 没使用索引的查询

 > db.test_4.find({name:'a'}).explain();
{
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 100001,
        "nscannedObjects" : 100001,
        "nscanned" : 100001,
        "nscannedObjectsAllPlans" : 100001,
        "nscannedAllPlans" : 100001,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 1,
        "nChunkSkips" : 0,
        "millis" : 156,
        "indexBounds" : {

        },
        "server" : "redhatB.example.com:27017"
}

   
  备注: indexBounds 为空表示没使用索引。


--7 强制查询使用索引

 db.test_4.find({name:'a'}).hint({skyid:1,name:1}).explain();
{
        "cursor" : "BtreeCursor idx_test_4_skyid_name",
        "isMultiKey" : false,
        "n" : 100001,
        "nscannedObjects" : 100001,
        "nscanned" : 100001,
        "nscannedObjectsAllPlans" : 100001,
        "nscannedAllPlans" : 100001,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 263,
        "indexBounds" : {
                "skyid" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ],
                "name" : [
                        [
                                "a",
                                "a"
                        ]
                ]
        },
        "server" : "redhatB.example.com:27017"
}

   备注:indexBounds 不为空表示已使用索引,并且 cursor 字段显示了使用的索引名称。
  

--8 参考
http://docs.mongodb.org/manual/reference/javascript/#cursor.explain
http://docs.mongodb.org/manual/reference/javascript/#cursor.hint