MongoDB数据库的索引操作(转)

Mongodb数据库的索引操作很简单,只需要把作为条件的字段设置为索引即可

> use user
switched to db user
> show collections
system.indexes
u_info
u_setting
> db.system.indexes.find();   这是默认的索引(默认为_id为索引)
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }

> db.u_info.insert({uid:1,name:"Falcon.C",address:"Beijing"});   
> db.u_info.insert({uid:2,name:"sexMan",address:"Wuhan"});    
> db.u_info.find();
{ "_id" : ObjectId("4b9cf280c84d7f20576c4df2"), "uid" : 1, "name" : "Falcon.C", "address" : "Beijing" }
{ "_id" : ObjectId("4b9cf284c84d7f20576c4df3"), "uid" : 2, "name" : "sexMan", "address" : "Wuhan" }

插入了2条记录,我们来把uid设置为索引字段:

> db.u_info.ensureIndex({uid:1});
> db.u_info.ensureIndex({name:1});
> db.system.indexes.find();
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
{ "ns" : "user.u_info", "key" : { "name" : 1 }, "name" : "name_1" }
>

这时我们看到多了刚才我们设置的那个字段,这样在查询的时候,如果查询条件有uid字段或name字段,则走索引来进行查询

有索引:

> db.u_info.find({name:"Falcon.C"});
{ "_id" : ObjectId("4b9cf280c84d7f20576c4df2"), "uid" : 1, "name" : "Falcon.C", "address" : "Beijing" }
> db.u_info.find({name:"Falcon.C"}).explain();
{
         "cursor" : "BtreeCursor name_1",
         "startKey" : {
                 "name" : "Falcon.C"
         },
         "endKey" : {
                 "name" : "Falcon.C"
         },
         "nscanned" : 1,
         "n" : 1,
         "millis" : 0,
         "allPlans" : [
                 {
                         "cursor" : "BtreeCursor name_1",
                         "startKey" : {
                                 "name" : "Falcon.C"
                         },
                         "endKey" : {
                                 "name" : "Falcon.C"
                         }
                 }
         ]
}

删除索引后:

> db.system.indexes.find();                   
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }
{ "ns" : "user.u_info", "key" : { "name" : 1 }, "name" : "name_1" }
> db.u_info.dropIndex("name_1")
{ "nIndexesWas" : 3, "ok" : 1 }
> db.u_info.find({name:"Falcon.C"}).explain();
{
         "cursor" : "BasicCursor",
         "startKey" : {

         },
         "endKey" : {

         },
         "nscanned" : 2,
         "n" : 1,
         "millis" : 0,
         "allPlans" : [
                 {
                         "cursor" : "BasicCursor",
                         "startKey" : {

                         },
                         "endKey" : {

                         }
                 }
         ]
}
> db.system.indexes.find();                   
{ "name" : "_id_", "ns" : "user.u_info", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "name" : "_id_", "ns" : "user.u_setting", "key" : { "_id" : ObjectId("000000000000000000000000") } }
{ "ns" : "user.u_info", "key" : { "uid" : 1 }, "name" : "uid_1" }

通过以上可以看出,查询的条件中有索引时,查询走BtreeCursor 的索引,而没有索引时走BasicCursor


通常需要索引的字段是:
1.唯一键 _id 是默认被设置为索引的
2.需要被查找的字段应该建立索引,比如在find()里面的字段
3.需要被排序的字段应该建立索引。比如在sort()里面的字段

以上就是MongoDB

的索引操作


本文转自 不得闲 博客园博客,原文链接: http://www.cnblogs.com/DxSoft/archive/2010/10/21/1857364.html  ,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值