码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB-性能优化之索引

时间:2015-03-21 22:48:14      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

  1. 首先看一个实例
>for(i=0;i<100000;i++){ db.indexdemo.insert({"i":i,"username":"user"+i,"age":Math.floor(Math.random()*120),"create":new Date});}
WriteResult({ "nInserted" : 1 })
> db.indexdemo.find()
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76444"), "i" : 0, "username" : "user0", "age" : 10, "create" : ISODate("2015-03-21T12:55:40.303Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76445"), "i" : 1, "username" : "user1", "age" : 53, "create" : ISODate("2015-03-21T12:55:40.377Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76446"), "i" : 2, "username" : "user2", "age" : 59, "create" : ISODate("2015-03-21T12:55:40.378Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76447"), "i" : 3, "username" : "user3", "age" : 63, "create" : ISODate("2015-03-21T12:55:40.381Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76448"), "i" : 4, "username" : "user4", "age" : 111, "create" : ISODate("2015-03-21T12:55:40.382Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76449"), "i" : 5, "username" : "user5", "age" : 16, "create" : ISODate("2015-03-21T12:55:40.383Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644a"), "i" : 6, "username" : "user6", "age" : 11, "create" : ISODate("2015-03-21T12:55:40.386Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644b"), "i" : 7, "username" : "user7", "age" : 3, "create" : ISODate("2015-03-21T12:55:40.387Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644c"), "i" : 8, "username" : "user8", "age" : 113, "create" : ISODate("2015-03-21T12:55:40.388Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644d"), "i" : 9, "username" : "user9", "age" : 73, "create" : ISODate("2015-03-21T12:55:40.390Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644e"), "i" : 10, "username" : "user10", "age" : 90, "create" : ISODate("2015-03-21T12:55:40.391Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644f"), "i" : 11, "username" : "user11", "age" : 15, "create" : ISODate("2015-03-21T12:55:40.391Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76450"), "i" : 12, "username" : "user12", "age" : 12, "create" : ISODate("2015-03-21T12:55:40.392Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76451"), "i" : 13, "username" : "user13", "age" : 41, "create" : ISODate("2015-03-21T12:55:40.393Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76452"), "i" : 14, "username" : "user14", "age" : 16, "create" : ISODate("2015-03-21T12:55:40.395Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76453"), "i" : 15, "username" : "user15", "age" : 111, "create" : ISODate("2015-03-21T12:55:40.395Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76454"), "i" : 16, "username" : "user16", "age" : 100, "create" : ISODate("2015-03-21T12:55:40.396Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76455"), "i" : 17, "username" : "user17", "age" : 78, "create" : ISODate("2015-03-21T12:55:40.396Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76456"), "i" : 18, "username" : "user18", "age" : 55, "create" : ISODate("2015-03-21T12:55:40.397Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76457"), "i" : 19, "username" : "user19", "age" : 57, "create" : ISODate("2015-03-21T12:55:40.397Z") }
Type "it" for more
> db.indexdemo.find({"username":"user101"}).explain()
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 100000,
    "nscanned" : 100000,
    "nscannedObjectsAllPlans" : 100000,
    "nscannedAllPlans" : 100000,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 781,
    "nChunkSkips" : 0,
    "millis" : 95,
    "server" : "timeless-HP-Pavilion-g4-Notebook-PC:27017",
    "filterSet" : false
}
> 

使用 limit()之后再看一个   可见使用limit 对于

> db.indexdemo.find({"username":"user101"}).limit(1).explain()
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 102,   //查询的时候要扫描的数量减少很多
    "nscanned" : 102,
    "nscannedObjectsAllPlans" : 102,
    "nscannedAllPlans" : 102,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 11,   //时间长度   减少很多
    "server" : "timeless-HP-Pavilion-g4-Notebook-PC:27017",
    "filterSet" : false
}

 接下来使用索引  db.xx.ensureIndex({"username":1});  可以看到查询是瞬间完成的 。

> db.
... indexdemo.ensureIndex({username:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.indexdemo.find({"username":"user101"}).explain()
{
    "cursor" : "BtreeCursor username_1",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 1,
    "nscannedAllPlans" : 1,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "username" : [
            [
                "user101",
                "user101"
            ]
        ]
    },
    "server" : "timeless-HP-Pavilion-g4-Notebook-PC:27017",
    "filterSet" : false
}

 

  1. 当数据非常多 ,创建索引很消耗时间的查询 创建索引需要在后台执行的 可以使用db.xx.ensureIndex({"username":1},{background:1});
  2. 可以创建类似于 mysql的 unique key 只需要在 ensureIndex({"username":1},{unique:true});   如果之前包含有非unique的数据则会出错。

 2、删除索引  

  • 使用 db.indexdemo.dropIndex({age:1});   //选择删除
  • 使用db.indexdemo.dropIndex();//表示全部删除

3、查看索引                     

  • db.indexdemo.getIndexs();
  • db.indexdemo.getIndexKyes();

4、 接下来介绍一下符合索引

MongoDB-性能优化之索引

标签:

原文地址:http://www.cnblogs.com/timelesszhuang/p/4356243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!