MongoDB-性能优化之索引
- 首先看一个实例
>for(i=;i<;i++){ db.indexdemo.insert({"i":i,"username":"user"+i,"age":Math.floor(Math.random()*),"create":new Date});}
WriteResult({ "nInserted" : })
> db.indexdemo.find()
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76444"), "i" : , "username" : "user0", "age" : , "create" : ISODate("2015-03-21T12:55:40.303Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76445"), "i" : , "username" : "user1", "age" : , "create" : ISODate("2015-03-21T12:55:40.377Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76446"), "i" : , "username" : "user2", "age" : , "create" : ISODate("2015-03-21T12:55:40.378Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76447"), "i" : , "username" : "user3", "age" : , "create" : ISODate("2015-03-21T12:55:40.381Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76448"), "i" : , "username" : "user4", "age" : , "create" : ISODate("2015-03-21T12:55:40.382Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76449"), "i" : , "username" : "user5", "age" : , "create" : ISODate("2015-03-21T12:55:40.383Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644a"), "i" : , "username" : "user6", "age" : , "create" : ISODate("2015-03-21T12:55:40.386Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644b"), "i" : , "username" : "user7", "age" : , "create" : ISODate("2015-03-21T12:55:40.387Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644c"), "i" : , "username" : "user8", "age" : , "create" : ISODate("2015-03-21T12:55:40.388Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644d"), "i" : , "username" : "user9", "age" : , "create" : ISODate("2015-03-21T12:55:40.390Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644e"), "i" : , "username" : "user10", "age" : , "create" : ISODate("2015-03-21T12:55:40.391Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff7644f"), "i" : , "username" : "user11", "age" : , "create" : ISODate("2015-03-21T12:55:40.391Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76450"), "i" : , "username" : "user12", "age" : , "create" : ISODate("2015-03-21T12:55:40.392Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76451"), "i" : , "username" : "user13", "age" : , "create" : ISODate("2015-03-21T12:55:40.393Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76452"), "i" : , "username" : "user14", "age" : , "create" : ISODate("2015-03-21T12:55:40.395Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76453"), "i" : , "username" : "user15", "age" : , "create" : ISODate("2015-03-21T12:55:40.395Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76454"), "i" : , "username" : "user16", "age" : , "create" : ISODate("2015-03-21T12:55:40.396Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76455"), "i" : , "username" : "user17", "age" : , "create" : ISODate("2015-03-21T12:55:40.396Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76456"), "i" : , "username" : "user18", "age" : , "create" : ISODate("2015-03-21T12:55:40.397Z") }
{ "_id" : ObjectId("550d6a4cd2474ebd4ff76457"), "i" : , "username" : "user19", "age" : , "create" : ISODate("2015-03-21T12:55:40.397Z") }
Type "it" for more
> db.indexdemo.find({"username":"user101"}).explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : ,
"nscannedObjects" : ,
"nscanned" : ,
"nscannedObjectsAllPlans" : ,
"nscannedAllPlans" : ,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : ,
"nChunkSkips" : ,
"millis" : ,
"server" : "timeless-HP-Pavilion-g4-Notebook-PC:27017",
"filterSet" : false
}
>
使用 limit()之后再看一个 可见使用limit 对于
> db.indexdemo.find({"username":"user101"}).limit().explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : ,
"nscannedObjects" : , //查询的时候要扫描的数量减少很多
"nscanned" : ,
"nscannedObjectsAllPlans" : ,
"nscannedAllPlans" : ,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : ,
"nChunkSkips" : ,
"millis" : , //时间长度 减少很多
"server" : "timeless-HP-Pavilion-g4-Notebook-PC:27017",
"filterSet" : false
}
接下来使用索引 db.xx.ensureIndex({"username":1}); 可以看到查询是瞬间完成的 。
> db.
... indexdemo.ensureIndex({username:})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : ,
"numIndexesAfter" : ,
"ok" :
}
> db.indexdemo.find({"username":"user101"}).explain()
{
"cursor" : "BtreeCursor username_1",
"isMultiKey" : false,
"n" : ,
"nscannedObjects" : ,
"nscanned" : ,
"nscannedObjectsAllPlans" : ,
"nscannedAllPlans" : ,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : ,
"nChunkSkips" : ,
"millis" : ,
"indexBounds" : {
"username" : [
[
"user101",
"user101"
]
]
},
"server" : "timeless-HP-Pavilion-g4-Notebook-PC:27017",
"filterSet" : false
}
- 当数据非常多 ,创建索引很消耗时间的查询 创建索引需要在后台执行的 可以使用db.xx.ensureIndex({"username":1},{background:1});
- 可以创建类似于 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-性能优化之索引的更多相关文章
- MongoDB 性能优化五个简单步骤
MongoDB 一直是最流行的 NoSQL,而根据 DB-Engines Ranking 最新的排行,时下 MongoDB 已经击败 PostgreSQL 跃居数据库总排行的第四位,仅次于 Oracl ...
- mongodb可以通过profile来监控数据 (mongodb性能优化)
mongodb可以通过profile来监控数据 (mongodb性能优化) 开启 Profiling 功能 ,对慢查询进行优化: mongodb可以通过profile来监控数据,进行优化. 查看 ...
- MySQL性能优化:索引
MySQL性能优化:索引 索引提供指向存储在表的指定列中的数据值的指针,然后根据您指定的排序顺序对这些指针排序.数据库使用索引以找到特定值,然后顺指针找到包含该值的行.这样可以使对应于表的SQL语句执 ...
- SQL Server数据库性能优化之索引篇【转】
http://www.blogjava.net/allen-zhe/archive/2010/07/23/326966.html 性能优化之索引篇 近期项目需要, 做了一段时间的SQL Server性 ...
- MySQL 数据库性能优化之索引优化
接着上一篇 MySQL 数据库性能优化之表结构,这是 MySQL数据库性能优化专题 系列的第三篇文章:MySQL 数据库性能优化之索引优化 大家都知道索引对于数据访问的性能有非常关键的作用,都知道索引 ...
- SQL Server查询性能优化——覆盖索引(二)
在SQL Server 查询性能优化——覆盖索引(一)中讲了覆盖索引的一些理论. 本文将具体讲一下使用不同索引对查询性能的影响. 下面通过实例,来查看不同的索引结构,如聚集索引.非聚集索引.组合索引等 ...
- MySQL查询性能优化七种武器之索引下推
前面已经讲了MySQL的其他查询性能优化方式,没看过可以去了解一下: MySQL查询性能优化七种武器之索引潜水 MySQL查询性能优化七种武器之链路追踪 今天要讲的是MySQL的另一种查询性能优化方式 ...
- MongoDB性能优化
一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引 ...
- MongoDB性能优化指南
一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引 ...
- 开发高性能的MongoDB应用—浅谈MongoDB性能优化(转)
出处:http://www.cnblogs.com/mokafamily/p/4102829.html 性能与用户量 “如何能让软件拥有更高的性能?”,我想这是一个大部分开发者都思考过的问题.性能往往 ...
随机推荐
- Delphi NativeXML 乱码的问题
我遇到 NativeXML 在它的一个节点的属性上面写入属性,但是当读出的值中包含汉字的时候出现了乱码.检查代码如下 NativeXml := TNativeXml.Create; try Nativ ...
- 6种GET和POST请求发送方法
我试过了畅言和多说两种社会化评论框,后来还是抛弃了畅言,不安全. 无论是畅言还是多说,我都需要从远程抓取文章的评论数,然后存入本地数据库.对于多说,请求的格式如下: // 获取评论次数,参数是文章ID ...
- P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)
题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M条小路在两个点之间.问从第一个点走到第N个点最短路是多少... 可以考虑在每一层增加一个点,这个点到 ...
- 必胜宅急送Web app设计背后的思考
O2O模式是餐饮业在移动消费趋势下主动拥抱互联网的方向,迎合餐饮消费者从以往经验判断为主转变为依靠移动设备.lbs.社交网络进行立体决策的过程.继App客户端之后,手机web app也逐渐成为O2O中 ...
- url传递中文的解决方案
本文转载:http://www.cnblogs.com/ghd258/archive/2005/10/23/260241.html url传递中文的解决方案 1.设置web.config文件. < ...
- 高效实现 std::string split() API
Qt下一个 QString 实现split()性能.和std::string未实现它的.STL也未实现.只有自己可以写一. #include <string> #include <v ...
- DBMS_RLS包实现数据库表中的行级安全控制
DBMS_RLS 实现一个数据库表为行级安全控制,该套餐包括细粒度的访问控制管理界面,此接口是用来实现VPD(Virtual Private Database),虚拟专用数据库.DBMS_RLS仅仅能 ...
- linux c 系统报错
本文中的错误是指在代码编译完全正确程序可运行的情况下,因为没有成功调用程序中的某些系统调用函数而产生的错误.往往这些系统调用函数通过返回值(比如1,0,-1)来说明其是否调用成功,而程序员需要知道详细 ...
- IO流--转载
第 1 部分 从输出流中读取 http://www.ibm.com/developerworks/cn/java/j-io1/ 自早期基于浏览器的 applet 和简单应用程序以来,Java 平台已有 ...
- 精准化测试专业平台Paw:苹果APP应用代码质量的守护者
众所周知,一款苹果APP进入苹果应用商店后,要上排行榜,要被首页推荐,版本更新必不可少,但作为版本更新的关键所在,软件测试一直被业内吐槽“坑太多”,缺陷防不胜防.相关经验缺乏等等.若问如何“填坑”?以 ...