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 性能与用户量 “如何能让软件拥有更高的性能?”,我想这是一个大部分开发者都思考过的问题.性能往往 ...
随机推荐
- HDOJ(HDU) 2139 Calculate the formula(水题,又一个用JavaAC不了的题目)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 看到这个时间,我懵逼了... 果然,J ...
- Sentry Server 日志记录分析服务
原文地址:http://blog.shanbay.com/archives/998 或许你不太会喜欢异常,特别是那些发生后继而沉默在应用日志里那些,你不知道从何开始,因为它们看起来并非那么平易近人,但 ...
- Ubuntu 12.04 Server OpenStack Havana多节点(OVS+GRE)安装
1.需求 节点角色 NICs 控制节点 eth0(10.10.10.51)eth1(192.168.100.51) 网络节点 eth0(10.10.10.52)eth1(10.20.20.52)eth ...
- HDU 1247
简单的字典树 - -,求一个单词是否由两个单词组成 #include<iostream> #include<cstring> #include<cstdio> us ...
- K - Candies(最短路+差分约束)
题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面 ...
- 那些年我用awk时踩过的坑——awk使用注意事项
由于项目经历原因,经常使用awk处理一些文本数据.甚至,我特意下载了一个windows上的awk:gawk.exe,这样在windows上也能享受awk处理数据的方便性,. 俗话说,"常在河 ...
- php 二维数组转换成树状数组(转)
<?php/** * @param array $list 要转换的结果集 * @param string $pid parent标记字段 * @param string $level leve ...
- [转] C++指针加整数、两个指针相减的问题
http://blog.csdn.net/onlyou930/article/details/6725051 说来惭愧,写C++有一段时间了.这个问题从来没有认真考虑过,此次标记于此: 考虑如下问题: ...
- POJ 2007 Scrambled Polygon 凸包
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7214 Accepted: 3445 ...
- C# WebService 基础实例
1.整个Demo结构:如下图: 2.新建项目--选择asp.net web服务应用程序TestWebService 3.重新命名Service1.asmx为MyService.asmx 4.右键MyS ...