Nosql Mongodb之旅(18)—MongoDB索引

 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样。其实可以这样说说,索引是凌驾于数据存储系统之上的另一层系统,所以各种结构迥异的存储都有相同或相似的索引实现及使用接口并不足为奇。

    基础索引

    在字段age 上创建索引,1(升序);-1(降序)

[plain] view plaincopy
  1. > db.t3.ensureIndex({age:1})  
  2. > db.t3.getIndexes();  
  3. [  
  4. {  
  5. "name" : "_id_",  
  6. "ns" : "test.t3",  
  7. "key" : {  
  8. "_id" : 1  
  9. },  
  10. "v" : 0  
  11. },  
  12. {  
  13. "_id" : ObjectId("4fb906da0be632163d0839fe"),  
  14. "ns" : "test.t3",  
  15. "key" : {  
  16. "age" : 1  
  17. },  
  18. "name" : "age_1",  
  19. "v" : 0  
  20. }  
  21. ]  
  22. >  

    上例显示出来的一共有2 个索引,其中_id 是创建表的时候自动创建的索引,此索引是不能够删除的。当系统已有大量数据时,创建索引就是个非常耗时的活,我们可以在后台执行,只需指定“backgroud:true”即可。

[plain] view plaincopy
  1. > db.t3.ensureIndex({age:1} , {backgroud:true})  

    文档索引
    索引可以任何类型的字段,甚至文档

[plain] view plaincopy
  1. db.factories.insert( { name: "wwl", addr: { city: "Beijing", state: "BJ" } } );  

    //在addr 列上创建索引

[plain] view plaincopy
  1. db.factories.ensureIndex( { addr : 1 } );  

    //下面这个查询将会用到我们刚刚建立的索引

[plain] view plaincopy
  1. db.factories.find( { addr: { city: "Beijing", state: "BJ" } } );  

    //但是下面这个查询将不会用到索引,因为查询的顺序跟索引建立的顺序不一样

[plain] view plaincopy
  1. db.factories.find( { addr: { state: "BJ" , city: "Beijing"} } );  

    组合索引

     跟其它数据库产品一样,MongoDB 也是有组合索引的,下面我们将在addr.city 和addr.state上建立组合索引。当创建组合索引时,字段后面的1 表示升序,-1 表示降序,是用1 还是用-1 主要是跟排序的时候或指定范围内查询 的时候有关的。

[plain] view plaincopy
  1. db.factories.ensureIndex( { "addr.city" : 1, "addr.state" : 1 } );  

    // 下面的查询都用到了这个索引

[plain] view plaincopy
  1. db.factories.find( { "addr.city" : "Beijing", "addr.state" : "BJ" } );  
  2. db.factories.find( { "addr.city" : "Beijing" } );  
  3. db.factories.find().sort( { "addr.city" : 1, "addr.state" : 1 } );  
  4. db.factories.find().sort( { "addr.city" : 1 } )  

    唯一索引
    只需在ensureIndex 命令中指定”unique:true”即可创建唯一索引。例如,往表t4 中插入2 条记录

[plain] view plaincopy
  1. db.t4.insert({firstname: "wang", lastname: "wenlong"});  
  2. db.t4.insert({firstname: "wang", lastname: "wenlong"});  

    在t4 表中建立唯一索引

[plain] view plaincopy
  1. > db.t4.ensureIndex({firstname: 1, lastname: 1}, {unique: true});  
  2. E11000 duplicate key error index: test.t4.$firstname_1_lastname_1 dup key: { : "wang", :  
  3. "wenlong" }  

    可以看到,当建唯一索引时,系统报了“表里有重复值”的错,具体原因就是因为表中有2条一模一模的数据,所以建立不了唯一索引。

    强制使用索引

    hint 命令可以强制使用某个索引。

[plain] view plaincopy
  1. > db.t5.insert({name: "wangwenlong",age: 20})  
  2. > db.t5.ensureIndex({name:1, age:1})  
  3. > db.t5.find({age:{$lt:30}}).explain()  
  4. {  
  5. "cursor" : "BasicCursor",  
  6. "nscanned" : 1,  
  7. "nscannedObjects" : 1,  
  8. "n" : 1,  
  9. "millis" : 0,  
  10. "nYields" : 0,  
  11. "nChunkSkips" : 0,  
  12. "isMultiKey" : false,  
  13. "indexOnly" : false,  
  14. "indexBounds" : { --并没有用到索引  
  15. }  
  16. }  
  17. > db.t5.find({age:{$lt:30}}).hint({name:1, age:1}).explain() --强制使用索引  
  18. {  
  19. "cursor" : "BtreeCursor name_1_age_1",  
  20. "nscanned" : 1,  
  21. "nscannedObjects" : 1,  
  22. "n" : 1,  
  23. "millis" : 1,  
  24. "nYields" : 0,  
  25. "nChunkSkips" : 0,  
  26. "isMultiKey" : false,  
  27. "indexOnly" : false,  
  28. "indexBounds" : { --被强制使用索引了  
  29. "name" : [  
  30. [  
  31. {  
  32. "$minElement" : 1  
  33. },  
  34. {  
  35. "$maxElement" : 1  
  36. }  
  37. ]  
  38. ],  
  39. "age" : [  
  40. [  
  41. -1.7976931348623157e+308,  
  42. 30  
  43. ]  
  44. ]  
  45. }  
  46. }  
  47. >  

    删除索引    

    删除索引分为删除某张表的所有索引和删除某张表的某个索引,具体如下:
    //删除t3 表中的所有索引

[plain] view plaincopy
  1. db.t3.dropIndexes()  

    //删除t4 表中的firstname 索引


  1. db.t4.dropIndex({firstname: 1})  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值