mongodb Index(2)

复合索引

> db.person.drop()

true

> for(var i=0;i<2000000;i++){< p="">

... db.person.insert({"name":"meteor"+i%1000,"age":20+i%10});

... }

WriteResult({ "nInserted" : 1 })

> db.person.ensureIndex({"age":1})        重新建立单一索引

{

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 1,

"numIndexesAfter" : 2,

"ok" : 1

}

> db.person.ensureIndex({"name":1,"age":1})        建立复合索引

{

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 2,

"numIndexesAfter" : 3,

"ok" : 1

}

> db.person.ensureIndex({"age":1,"name":1})        建立复合索引

{

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 3,

"numIndexesAfter" : 4,

"ok" : 1

}

> db.person.find({"age":{"$gte":20,"$lte":30},"name":"meteor1"}).hint({"age":1}).explain("executionStats")

{

"queryPlanner" : {

"plannerVersion" : 1,

"namespace" : "test.person",

"indexFilterSet" : false,

"parsedQuery" : {

"$and" : [

{

"name" : {

"$eq" : "meteor1"

}

},

{

"age" : {

"$lte" : 30

}

},

{

"age" : {

"$gte" : 20

}

}

]

},

"winningPlan" : {

"stage" : "FETCH",

"filter" : {

"name" : {

"$eq" : "meteor1"

}

},

"inputStage" : {

"stage" : "IXSCAN",

"keyPattern" : {

"age" : 1

},

"indexName" : "age_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"age" : [

"[20.0, 30.0]"

]

}

}

},

"rejectedPlans" : [ ]

},

"executionStats" : {

"executionSuccess" : true,

"nReturned" : 2000,

"executionTimeMillis" : 2657,

"totalKeysExamined" : 2000000,

"totalDocsExamined" : 2000000,

"executionStages" : {

"stage" : "FETCH",

"filter" : {

"name" : {

"$eq" : "meteor1"

}

},

"nReturned" : 2000,

"executionTimeMillisEstimate" : 2130,

"works" : 2000001,

"advanced" : 2000,

"needTime" : 1998000,

"needYield" : 0,

"saveState" : 15625,

"restoreState" : 15625,

"isEOF" : 1,

"invalidates" : 0,

"docsExamined" : 2000000,

"alreadyHasObj" : 0,

"inputStage" : {

"stage" : "IXSCAN",

"nReturned" : 2000000,

"executionTimeMillisEstimate" : 620,

"works" : 2000001,

"advanced" : 2000000,

"needTime" : 0,

"needYield" : 0,

"saveState" : 15625,

"restoreState" : 15625,

"isEOF" : 1,

"invalidates" : 0,

"keyPattern" : {

"age" : 1

},

"indexName" : "age_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"age" : [

"[20.0, 30.0]"

]

},

"keysExamined" : 2000000,

"dupsTested" : 0,

"dupsDropped" : 0,

"seenInvalidated" : 0

}

}

},

"serverInfo" : {

"host" : "meteor.yeecall.com",

"port" : 27027,

"version" : "3.2.8",

"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"

},

"ok" : 1

}

>

> db.person.find({"age":{"$gte":20,"$lte":30},"name":"meteor1"}).hint({"age":1,"name":1}).explain("executionStats")

{

"queryPlanner" : {

"plannerVersion" : 1,

"namespace" : "test.person",

"indexFilterSet" : false,

"parsedQuery" : {

"$and" : [

{

"name" : {

"$eq" : "meteor1"

}

},

{

"age" : {

"$lte" : 30

}

},

{

"age" : {

"$gte" : 20

}

}

]

},

"winningPlan" : {

"stage" : "FETCH",

"inputStage" : {

"stage" : "IXSCAN",

"keyPattern" : {

"age" : 1,

"name" : 1

},

"indexName" : "age_1_name_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"age" : [

"[20.0, 30.0]"

],

"name" : [

"[\\"meteor1\\", \\"meteor1\\"]"

]

}

}

},

"rejectedPlans" : [ ]

},

"executionStats" : {

"executionSuccess" : true,

"nReturned" : 2000,

"executionTimeMillis" : 16,        根据查询语句的不同,建立正确的索引是非常重要的,对于查询语句中是多条件的,应多考虑复合索引的应用

"totalKeysExamined" : 2010,

"totalDocsExamined" : 2000,

"executionStages" : {

"stage" : "FETCH",

"nReturned" : 2000,

"executionTimeMillisEstimate" : 20,

"works" : 2011,

"advanced" : 2000,

"needTime" : 10,

"needYield" : 0,

"saveState" : 15,

"restoreState" : 15,

"isEOF" : 1,

"invalidates" : 0,

"docsExamined" : 2000,

"alreadyHasObj" : 0,

"inputStage" : {

"stage" : "IXSCAN",

"nReturned" : 2000,

"executionTimeMillisEstimate" : 20,

"works" : 2011,

"advanced" : 2000,

"needTime" : 10,

"needYield" : 0,

"saveState" : 15,

"restoreState" : 15,

"isEOF" : 1,

"invalidates" : 0,

"keyPattern" : {

"age" : 1,

"name" : 1

},

"indexName" : "age_1_name_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"age" : [

"[20.0, 30.0]"

],

"name" : [

"[\\"meteor1\\", \\"meteor1\\"]"

]

},

"keysExamined" : 2010,

"dupsTested" : 0,

"dupsDropped" : 0,

"seenInvalidated" : 0

}

}

},

"serverInfo" : {

"host" : "meteor.yeecall.com",

"port" : 27027,

"version" : "3.2.8",

"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"

},

"ok" : 1

}

>

> db.person.find({"age":{"$gte":20.0,"$lte":30.0}}).sort({"name":1}).limit(100).hint({"age":1,"name":1}).explain("executionStats")

{

"queryPlanner" : {

"plannerVersion" : 1,

"namespace" : "test.person",

"indexFilterSet" : false,

"parsedQuery" : {

"$and" : [

{

"age" : {

"$lte" : 30

}

},

{

"age" : {

"$gte" : 20

}

}

]

},

"winningPlan" : {

"stage" : "SORT",

"sortPattern" : {

"name" : 1

},

"limitAmount" : 100,

"inputStage" : {

"stage" : "SORT_KEY_GENERATOR",

"inputStage" : {

"stage" : "FETCH",

"inputStage" : {

"stage" : "IXSCAN",

"keyPattern" : {

"age" : 1,

"name" : 1

},

"indexName" : "age_1_name_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"age" : [

"[20.0, 30.0]"

],

"name" : [

"[MinKey, MaxKey]"

]

}

}

}

}

},

"rejectedPlans" : [ ]

},

"executionStats" : {

"executionSuccess" : true,

"nReturned" : 100,

"executionTimeMillis" : 7147,

"totalKeysExamined" : 2000000,

"totalDocsExamined" : 2000000,

"executionStages" : {

"stage" : "SORT",

"nReturned" : 100,

"executionTimeMillisEstimate" : 6220,

"works" : 2000103,

"advanced" : 100,

"needTime" : 2000002,

"needYield" : 0,

"saveState" : 15625,

"restoreState" : 15625,

"isEOF" : 1,

"invalidates" : 0,

"sortPattern" : {

"name" : 1

},

"memUsage" : 6100,

"memLimit" : 33554432,

"limitAmount" : 100,

"inputStage" : {

"stage" : "SORT_KEY_GENERATOR",

"nReturned" : 0,

"executionTimeMillisEstimate" : 5740,

"works" : 2000002,

"advanced" : 0,

"needTime" : 1,

"needYield" : 0,

"saveState" : 15625,

"restoreState" : 15625,

"isEOF" : 1,

"invalidates" : 0,

"inputStage" : {

"stage" : "FETCH",

"nReturned" : 2000000,

"executionTimeMillisEstimate" : 4690,

"works" : 2000001,

"advanced" : 2000000,

"needTime" : 0,

"needYield" : 0,

"saveState" : 15625,

"restoreState" : 15625,

"isEOF" : 1,

"invalidates" : 0,

"docsExamined" : 2000000,

"alreadyHasObj" : 0,

"inputStage" : {

"stage" : "IXSCAN",

"nReturned" : 2000000,

"executionTimeMillisEstimate" : 1650,

"works" : 2000001,

"advanced" : 2000000,

"needTime" : 0,

"needYield" : 0,

"saveState" : 15625,

"restoreState" : 15625,

"isEOF" : 1,

"invalidates" : 0,

"keyPattern" : {

"age" : 1,

"name" : 1

},

"indexName" : "age_1_name_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"age" : [

"[20.0, 30.0]"

],

"name" : [

"[MinKey, MaxKey]"

]

},

"keysExamined" : 2000000,

"dupsTested" : 0,

"dupsDropped" : 0,

"seenInvalidated" : 0

}

}

}

}

},

"serverInfo" : {

"host" : "meteor.yeecall.com",

"port" : 27027,

"version" : "3.2.8",

"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"

},

"ok" : 1

}

> db.person.find({"age":{"$gte":20.0,"$lte":30.0}}).sort({"name":1}).limit(100).hint({"name":1,"age":1}).explain("executionStats")

{

"queryPlanner" : {

"plannerVersion" : 1,

"namespace" : "test.person",

"indexFilterSet" : false,

"parsedQuery" : {

"$and" : [

{

"age" : {

"$lte" : 30

}

},

{

"age" : {

"$gte" : 20

}

}

]

},

"winningPlan" : {

"stage" : "LIMIT",

"limitAmount" : 100,

"inputStage" : {

"stage" : "FETCH",

"filter" : {

"$and" : [

{

"age" : {

"$lte" : 30

}

},

{

"age" : {

"$gte" : 20

}

}

]

},

"inputStage" : {

"stage" : "IXSCAN",

"keyPattern" : {

"name" : 1,

"age" : 1

},

"indexName" : "name_1_age_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"name" : [

"[MinKey, MaxKey]"

],

"age" : [

"[MinKey, MaxKey]"

]

}

}

}

},

"rejectedPlans" : [ ]

},

"executionStats" : {

"executionSuccess" : true,

"nReturned" : 100,

"executionTimeMillis" : 0,

"totalKeysExamined" : 100,

"totalDocsExamined" : 100,

"executionStages" : {

"stage" : "LIMIT",

"nReturned" : 100,

"executionTimeMillisEstimate" : 0,

"works" : 101,

"advanced" : 100,

"needTime" : 0,

"needYield" : 0,

"saveState" : 0,

"restoreState" : 0,

"isEOF" : 1,

"invalidates" : 0,

"limitAmount" : 100,

"inputStage" : {

"stage" : "FETCH",

"filter" : {

"$and" : [

{

"age" : {

"$lte" : 30

}

},

{

"age" : {

"$gte" : 20

}

}

]

},

"nReturned" : 100,

"executionTimeMillisEstimate" : 0,

"works" : 100,

"advanced" : 100,

"needTime" : 0,

"needYield" : 0,

"saveState" : 0,

"restoreState" : 0,

"isEOF" : 0,

"invalidates" : 0,

"docsExamined" : 100,

"alreadyHasObj" : 0,

"inputStage" : {

"stage" : "IXSCAN",

"nReturned" : 100,

"executionTimeMillisEstimate" : 0,

"works" : 100,

"advanced" : 100,

"needTime" : 0,

"needYield" : 0,

"saveState" : 0,

"restoreState" : 0,

"isEOF" : 0,

"invalidates" : 0,

"keyPattern" : {

"name" : 1,

"age" : 1

},

"indexName" : "name_1_age_1",

"isMultiKey" : false,

"isUnique" : false,

"isSparse" : false,

"isPartial" : false,

"indexVersion" : 1,

"direction" : "forward",

"indexBounds" : {

"name" : [

"[MinKey, MaxKey]"

],

"age" : [

"[MinKey, MaxKey]"

]

},

"keysExamined" : 100,

"dupsTested" : 0,

"dupsDropped" : 0,

"seenInvalidated" : 0

}

}

}

},

"serverInfo" : {

"host" : "meteor.yeecall.com",

"port" : 27027,

"version" : "3.2.8",

"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"

},

"ok" : 1

}

>分析:第一种索引,需要找到所有复合查询条件的值(依据索引,键和文档可以快速找到),但是找到后,需要对文档在内存中进行排序,这个步骤消耗了非常多的时间。第二种索引,效果非常好,因为不需要在内存中对大量数据进行排序。但是,MongoDB不得不扫描整个索引以便找到所有文档。因此,如果对查询结果的范围做了限制,那么MongoDB在几次匹配之后就可以不再扫描索引,在这种情况下,将排序键放在第一位是一个非常好的策略。

> db.person.getIndexes()        查看索引

[

{

"v" : 1,

"key" : {

"_id" : 1

},

"name" : "_id_",

"ns" : "test.person"

},

{

"v" : 1,

"key" : {

"age" : 1

},

"name" : "age_1",

"ns" : "test.person"

},

{

"v" : 1,

"key" : {

"name" : 1,

"age" : 1

},

"name" : "name_1_age_1",

"ns" : "test.person"

},

{

"v" : 1,

"key" : {

"age" : 1,

"name" : 1

},

"name" : "age_1_name_1",

"ns" : "test.person"

}

]

>

> db.person.dropIndex("age_1")        删除索引

{ "nIndexesWas" : 4, "ok" : 1 }

> db.person.dropIndex("name_1_age_1")

{ "nIndexesWas" : 3, "ok" : 1 }

> db.person.dropIndex("age_1_name_1")

{ "nIndexesWas" : 2, "ok" : 1 }

>

> db.person.ensureIndex({"name":1,"age":1},{"unique":true});        创建唯一索引


MongoDB 索引限制

额外开销

每个索引占据一定的存储空间,在进行插入,更新和删除操作时也需要对索引进行操作。所以,如果你很少对集合进行读取操作,建议不使用索引。

内存(RAM)使用

由于索引是存储在内存(RAM)中,你应该确保该索引的大小不超过内存的限制。

如果索引的大小大于内存的限制,MongoDB会删除一些索引,这将导致性能下降。

查询限制

索引不能被以下的查询使用:正则表达式及非操作符,如 $nin, $not, 等;算术运算符,如 $mod, 等;$where 子句

所以,检测语句是否使用索引是一个好的习惯,可以用explain来查看。

索引键限制

从2.6版本开始,如果现有的索引字段的值超过索引键的限制,MongoDB中不会创建索引。

插入文档超过索引键限制

如果文档的索引字段值超过了索引键的限制,MongoDB不会将任何文档转换成索引的集合。与mongorestore和mongoimport工具类似。

最大范围

集合中索引不能超过64个;索引名的长度不能超过125个字符

一个复合索引最多可以有31个字段

时间: 2024-11-01 03:45:27

mongodb Index(2)的相关文章

mongodb Index(3)

全文索引 MongoDB有一个特殊的索引用在文档中搜索文本,之前的博客都是用精确匹配来查询字符串,这些技术有一定的限制.在搜索大块文本的速度非常慢,而且无法处理自然语言礼节的问题.全文本索引使用的是"倒排索引"的思想来做的,和当前非常开源的lucene(全文检索)项目是一样的思想来实现的.使用全文本索引可以非常快的进行文本搜索,MongoDB支持多种语言,可惜在免费版中,并不支持汉语.查MongoDB的官网可以看到,在企业版中安装第三方插件可以支持中文的全文索引. 使用全文本检索需要专

mongodb index 的background 及集群的索引建立

在数据库建立索引时,默认时"foreground" 也就是前台建立索引,但是,当你的数据库数据量很大时,在建立索引的时会读取数据文件,大量的文件读写会阻止其他的操作,此时在建立索引的时候用background:true,虽然,background在可以在后台建立索引. 在后台建立索引的时候,不能对建立索引的collection进行一些坏灭型的操作,如:运行repairDatabase,drop,compat,当你在建立索引的时候运行这些操作的会报错. Building Indexes

MongoDB 使用Index

Index 能够提高查询的性能,如果没有Index,MongoDB必须扫描整个collection,从collection的第一个doc开始,直到最后一个doc,即使第一个doc之后的所有doc都不满足查询条件.如果在Collection上有合适的Index,例如,unique index,那么MongoDB在按照index key查找到一个doc之后,就不会继续对其他doc查询,极大地提高查询性能. MongoDB的 Index 结构跟关系型DB的NonClustered Index相似,都是

MongoDB之TextSearch简介

MongoDB之TextSearch简介  MongoDB支持对文本内容执行文本搜索操作,其提供了索引text index和查询操作$text来完成文本搜索功能.下面我们通过一个简单的例子来体验一下MongoDB提供的全文检索功能. 1.新建blogs collection,并插入如下的document. db.blogs.insert({_id:1,title:"MongoDB text search",content:"this is a simple MongoDB t

Nagios监控mongodb分片集群服务实战

1,监控插件下载 Mongodb插件下载地址为:git clone git://github.com/mzupan/nagios-plugin-mongodb.git,刚开始本人这里没有安装gitpub环境,找网友草根帮忙下载的,之后上传到了csdn资源页面,新的下载地址为:http://download.csdn.net/detail/mchdba/8019077 2,添加新的mongodb监控命令 因为mongodb服务是和mysql从库公用一台物理机,之前已经做了基础nagios以及mys

[Cacti] mongodb性能监控实战

前言: 为了更好的使用mongodb,需要监控出mongodb的一些基础使用情况,比如Flush数.连接数.内存使用率.Index操作,Slave延迟等等,这些可以通过配置cacti监控mongodb的模板来完成.  1,在cacti界面导入模板 在计算机本地,下载此tgz包:http://mysql-cacti-templates.googlecode.com/files/better-cacti-templates-1.1.8.tar.gz 解压到任意目录,然后打开cacti的web界面,选

MongoDB基础教程系列--第七篇 MongoDB 聚合管道

在讲解聚合管道(Aggregation Pipeline)之前,我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最大值.最小值.平均值,求和等操作.聚合操作还能够对记录进行复杂的操作,主要用于数理统计和数据挖掘.在 MongoDB 中,聚合操作的输入是集合中的文档,输出可以是一个文档,也可以是多条文档. MongoDB 提供了非常强大的聚合操作,有三种方式: 聚合管道(Aggregation Pipeline)

mongodb的处理

转自:http://www.cnblogs.com/cswuyg/p/4355948.html 1.count统计结果错误 这是由于分布式集群正在迁移数据,它导致count结果值错误,需要使用aggregate pipeline来得到正确统计结果,例如: db.collection.aggregate([{$group: {_id: null, count: {$sum: 1}}}]) 引用:“On a sharded cluster, count can result in an inaccu

结合MongoDB开发LBS应用

?简介 随着近几年各类移动终端的迅速普及,基于地理位置的服务(LBS)和相关应用也越来越多,而支撑这些应用的最基础技术之一,就是基于地理位置信息的处理.我所在的项目也正从事相关系统的开发,我们使用的是Symfony2+Doctrine2 ODM+MongoDB的组合. 我们将这些技术要点整理成文,希望能够通过本文的介绍和案例,详细解释如何使用MongoDB进行地理位置信息的查询和处理.在文章的开头,我们也会先介绍一下业界通常用来处理地理位置信息的一些方案并进行比较,让读者逐步了解使用MongoD