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 性能与用户量 “如何能让软件拥有更高的性能?”,我想这是一个大部分开发者都思考过的问题.性能往往 ...
随机推荐
- kafka中对一个topic增加replicas
是指手动写扩充replicas的配置文件,然后使用工具进行操作. 参考官网site:http://kafka.apache.org/documentation.html#basic_ops_autom ...
- FZYZOJ-1569 喝水
P1569 -- 喝水 时间限制:2000MS 内存限制:131072KB 状态:Accepted 标签: 无 无 无 Description GH的N个妹子要喝水, ...
- SRM 400(1-250pt, 1-500pt)
DIV1 250pt 题意:给定一个正整数n(n <= 10^18),如果n = p^q,其中p为质数,q > 1,则返回vector<int> ans = {p, q},否则 ...
- MAVEN 工程打包resources目录外的更多资源文件
首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,ma ...
- linux 编译安装apache
1.下载apache.安装apache #wget http://apache.etoak.com//httpd/httpd-2.4.4.tar.gz #tar zxvf httpd-2.4..4.t ...
- Beautiful Numbers
http://codeforces.com/problemset/problem/300/C 题意:给你三个数a,b,n;求满足由a,b组成的n位的个数,且每个位置上的数之和也是用a,b组成; 解析: ...
- redis学习心得之三-【java操作redis】
今天主要是讲讲java对redis的操作,来段代码掩饰下基本操作即可明白. java调用你需要下载jedis.jar包 下载网址:https://github.com/xetorthio/jedis/ ...
- 《开源分享2》:《开源框架实战宝典电子书V1.0.0》完整版!
经过一个多月的整理,<J2EE开源框架实战宝典>--Tiny文档PDF电子书開始发放,共同拥有将近600页.为喜爱Tiny.热爱Java开源框架的朋友提供更加体贴的文档服务! 下载地址:h ...
- 网络接口 使用NSURLConnection完成Get和Post方法
网络接口 使用NSURLConnection完成Get和Post方法 什么是URL: URL就是统一资源定位器(UniformResourceLocator:URL).通俗地说,它是用来指出某一项信息 ...
- 自定义控件(视图)2期笔记09:自定义视图之继承自ViewGroup(仿ViewPager效果案例)
1. 这里我们继承已有ViewGroup实现自定义控件,模拟出来ViewPager的效果,如下: (1)实现的效果图如下: (2)实现步骤: • 自定义view继承viewGroup • 重写onLa ...