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 性能与用户量 “如何能让软件拥有更高的性能?”,我想这是一个大部分开发者都思考过的问题.性能往往 ...
随机推荐
- Invert Binary Tree——LeetCode
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- Vagrant虚拟机的配置管理
Vagrant虚拟机的配置管理 一.shell配置管理 二.使用Puppet进行配置管理 三.案例 Apache服务器的自动配置 3.1 shell配置管理 3.2 puppet配置管理 ps:由于最 ...
- HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】
根据题目意思,很容易得出,一个区间里面连续的段数即为最少的group数. 题解上面给的是用树状数组维护的. 询问一个区间的时候,可以一个一个的向里面添加,只需要判断a[i]-1 和 a[i]+1是否已 ...
- ORA-01078:failure in processing system parameters
一.使用环境操作系统:rhel 6.5 x64数据库:Oracle 11.2.0.1.0数据库主目录:/u01/app/oracle/product/11.2.0/ 二.问题描述用sys用户登录sql ...
- Centos 多个mysql数据库
一.编译安装第一个MySQL 5.1.33 ? cd /opt /usr/sbin/groupadd mysql /usr/sbin/useradd -g mysql mysql -s /bi ...
- OpenCV 编码样式指南
OpenCV 编码样式指南 前言 本文档是对OpenCV中代码风格的简短说明,因为OpenCV的核心库(cv,cvaux)是用C和C++编写的,所以本文档仅对用C和C++代码的编写有效. 文件 ...
- Flashback Query、Flashback Table(快速闪回查询、快速闪回表)
Flashback Query闪回查询 flashback query是基于undo表空间的闪回,与之相关的参数如下: SQL> show parameter undo NAME ...
- 模仿ios下的coverflow
Android高级图片滚动控件,编写3D版的图片轮播器 http://blog.csdn.net/guolin_blog/article/details/17482089 A cool Open So ...
- Linux shell入门基础(四)
四.进程优先级前台后台 01.进程控制 #find /name aaa & #ps aux | grep find #updatedb & #ps aux | grep update ...