惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Security Affairs
PCI Perspectives
PCI Perspectives
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Privacy & Cybersecurity Law Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
V
Visual Studio Blog
博客园 - 聂微东
Scott Helme
Scott Helme
博客园 - 【当耐特】
K
Kaspersky official blog
Security Latest
Security Latest
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
博客园 - 叶小钗
C
Check Point Blog
V2EX - 技术
V2EX - 技术
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
T
Tor Project blog
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
雷峰网
雷峰网
博客园_首页
美团技术团队
Y
Y Combinator Blog
C
CERT Recently Published Vulnerability Notes
AWS News Blog
AWS News Blog
月光博客
月光博客
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
C
Cybersecurity and Infrastructure Security Agency CISA

博客园 - K!ngZ

常用正则 MongoDB学习笔记(9)--优化器 profile MongoDB学习笔记(7)--访问控制 MongoDB学习笔记(6)--数据备份数据恢复 MongoDB学习笔记(5)--数据导入导出mongoexport MongoDB学习笔记(4)--Capped Collection MongoDB学习笔记(3)--高级查询 MongoDB学习笔记(2)--增删改查 MongoDB学习笔记(1)--了解MongoDB 远程文件抓取类 [转]BizTalk开发系列(三十四) Xpath SQL Server 2005 命令行实用工具 [转]雅虎公司C#笔试题 [转] OLE DB 访问接口 "SQLNCLI" 返回了消息 "未指定的错误"。 Asp.net中的日期处理函数 [转]正则表达式实现资料验证的技术总结 ajax乱码问题解决 [转]ASP.Net缓存总结 [转]预防按钮的多次点击【恶意刷新】
MongoDB学习笔记(8)--索引及优化索引
K!ngZ · 2011-09-15 · via 博客园 - K!ngZ

MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes中,且默认总是为_id创建索引,它的索引使用基本和 MySQL 等关系型数据库一样。其实可以这样说说,索引是凌驾于数据存储系统之上的另一层系统,所以各种结构迥异的存储都有相同或相似的索引实现及使用接口并不足为奇。

基础索引 

在字段age上创建索引,1(升序);-1(降序)  

 db.t3.ensureIndex({age:1})  

 db.t3.getIndexes();  

_id是创建表的时候自动创建的索引,此索引是不能够删除的。 

当系统已有大量数据时,创建索引就是个非常耗时的活,我们可以在后台执行,只需指定“backgroud:true”即可。  

 db.t3.ensureIndex({age:1} , {backgroud:true})  

文档索引 

 索引可以任何类型的字段,甚至文档

 在addr 列上创建索引 

 db.factories.ensureIndex( { addr : 1 } );  

 db.factories.find( { addr: { city: "Beijing", state: "BJ" } } );  

 但是下面这个查询将不会用到索引,因为查询的顺序跟索引建立的顺序不一样

 db.factories.find( { addr: { state: "BJ" , city: "Beijing"} } ); 

组合索引 

跟其它数据库产品一样,MongoDB 也是有组合索引的,下面我们将在addr.city 和addr.state

上建立组合索引。当创建组合索引时,字段后面的 1 表示升序,-1 表示降序,是用 1 还是

用-1主要是跟排序的时候或指定范围内查询  的时候有关的。 

db.factories.ensureIndex( { "addr.city" : 1, "addr.state" : 1 } );

//  下面的查询都用到了这个索引 

db.factories.find( { "addr.city" : "Beijing", "addr .state" : "BJ" } ); 

db.factories.find( { "addr.city" : "Beijing" } ); 

db.factories.find().sort( { "addr.city" : 1, "addr.state" : 1 } ); 

db.factories.find().sort( { "addr.city" : 1 } )  

唯一索引 

只需在ensureIndex命令中指定”unique:true”即可创建唯一索引。 

db.t4.ensureIndex({firstname: 1, lastname: 1}, {unique: true});  

强制使用索引

 db.t5.find({age:{$lt:30}}).hint({name:1, age:1})

删除索引 

 删除 t3表中的所有索引 

 db.t3.dropIndexes()  

 删除 t4表中的 firstname 索引  

 db.t4.dropIndex({firstname: 1}) 

explain 执行计划

 MongoDB  提供了一个 explain  命令让我们获知系统如何处理查询请求。利用 explain  命令,我们可以很好地观察系统如何使用索引来加快检索,同时可以针对性优化索引。

  db.t5.find({age:{$gt:45}}, {name:1}).explain()         

        "cursor" : "BtreeCursor age_1", 

        "nscanned" : 0, 

        "nscannedObjects" : 0, 

        "n" : 0, 

        "millis" : 0, 

        "nYields" : 0, 

        "nChunkSkips" : 0, 

        "isMultiKey" : false, 

        "indexOnly" : false, 

        "indexBounds" : { 

                "age" : [ 

                        [ 

                                45, 

                                1.7976931348623157e+308 

                        ] 

                ] 

}

字段说明

cursor:  返回游标类型(BasicCursor  或 BtreeCursor)  

nscanned:  被扫描的文档数 

n:  返回的文档数量  

millis:  耗时(毫秒)  

indexBounds:  所使用的索引