前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB 索引

MongoDB 索引

作者头像
阳光岛主
发布2019-02-18 16:15:32
5370
发布2019-02-18 16:15:32
举报
文章被收录于专栏:米扑专栏米扑专栏

1. ensureIndex添加索引  ensureIndex 函数帮助文档  db.blog.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups  name:指定索引名称  unique:是否唯一索引  dropDups:是否删除重复  创建索引的缺点:每次插入、更新、删除时都会产生额外的开销,要尽可能少创建索引。每个集合默认的最大索引个数为64个。  如果没有对应的键,索引会将其作为null存储,所以,如果对某个建立了唯一索引,但插入了多个缺少该索引键的文档,则由于文档包含null值而导致插入失败。  例子  > db.users.find()  { "_id" : ObjectId("4fc6d0c9387a7fee4eb6bfa9"), "name" : "aaa", "age" : 23, "sex" : "male" }  { "_id" : ObjectId("4fc6d0e5387a7fee4eb6bfaa"), "name" : "bbb", "age" : 25, "sex" : "male" }  { "_id" : ObjectId("4fc6d0f4387a7fee4eb6bfab"), "name" : "ccc", "age" : 25, "sex" : "male" }  { "_id" : ObjectId("4fc6d100387a7fee4eb6bfac"), "name" : "ddd", "age" : 25, "sex" : "male" }  { "_id" : ObjectId("4fc6d110387a7fee4eb6bfad"), "name" : "eee", "age" : 23, "sex" : "male" }  > db.users.ensureIndex({"name":1,"age":-1},{"name":"userIndex"})  //1,-1代表索引方向  //查找索引  > db.system.indexes.find()  { "name" : "_id_", "ns" : "blog.users", "key" : { "_id" : 1 }, "v" : 0 }  { "_id" : ObjectId("4fc6d1d0387a7fee4eb6bfb1"), "ns" : "blog.users", "key" : { "name" : 1, "age" : -1 }, "name" : "userIndex", "v" : 0 } 

2. explain()监控索引使用情况  > db.users.find({"name":"qff","age":23}).explain()  {          "cursor" : "BtreeCursor userIndex",//没有使用索引时为BasicCursor,索引存储在B树结构中,所以使用查询时候会使用BtreeCursor类型的游标,userIndex为使用索引的名称          "nscanned" : 1,//查找了多少个文档          "nscannedObjects" : 1,          "n" : 1,//返回文档的数量          "millis" : 50,//数据库执行查询时间          "nYields" : 0,          "nChunkSkips" : 0,          "isMultiKey" : false,          "indexOnly" : false,          "indexBounds" : {                  "name" : [                          [                                  "qff",                                  "qff"                          ]                  ],                  "age" : [                          [                                  23,                                  23                          ]                  ]          }  } 

3. hint强制使用某个索引  > db.users.find({"age":23,"name":/.*/}).hint({"name":1,"age":-1})  { "_id" : ObjectId("4fc6d110387a7fee4eb6bfad"), "name" : "aaa", "age" : 23, "sex  " : "male" }  { "_id" : ObjectId("4fc6d121387a7fee4eb6bfaf"), "name" : "bbb", "age" : 23, "se  x" : "male" }  { "_id" : ObjectId("4fc6d11b387a7fee4eb6bfae"), "name" : "ccc", "age" : 23, "sex  " : "male" }  { "_id" : ObjectId("4fc6d12b387a7fee4eb6bfb0"), "name" : "ddd", "age" : 23, "se  x" : "male" }  { "_id" : ObjectId("4fc6d0c9387a7fee4eb6bfa9"), "name" : "eee", "age" : 23, "  sex" : "male" }  MongoDB的查询优化器非常智能,会替你选择该用哪个索引,多数情况下不需要指定的。 

参考推荐:

Create a Unique Index(官方文档)

MongoDB 索引

MongoDB基本管理命令

MongoDB 常用命令

MongoDB 基本操作

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年01月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档