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

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - K!ngZ

常用正则 MongoDB学习笔记(8)--索引及优化索引 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学习笔记(9)--优化器 profile
K!ngZ · 2011-09-15 · via 博客园 - K!ngZ

在MySQL中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是MongoDB Database Profiler。所以MongoDB 不仅有,而且还有一些比MySQL的Slow Query Log更详细的信息。

开启 Profiling  功能 

有两种方式可以控制 Profiling  的开关和级别,第一种是直接在启动参数里直接进行设置。 启动MongoDB时加上–profile=级别  即可。 

也可以在客户端调用 db.setProfilingLevel(级别)  命令来实时配置,Profiler  信息保存在 system.profile 中。我们可以通过db.getProfilingLevel()命令来获取当前的Profile级别,类似如下操作 

 db.setProfilingLevel(2);    

上面profile的级别可以取0,1,2  三个值,他们表示的意义如下: 

0 –  不开启 

1 –  记录慢命令 (默认为>100ms)  

2 –  记录所有命令  

Profile  记录在级别 1 时会记录慢命令,那么这个慢的定义是什么?上面我们说到其默认为100ms,当然有默认就有设置,其设置方法和级别一样有两种,一种是通过添加–slowms 启动参数配置。第二种是调用db.setProfilingLevel时加上第二个参数: 

db.setProfilingLevel( 1 , 10 ); 

查询 Profiling  记录 

与 MySQL 的慢查询日志不同,MongoDB Profile  记录是直接存在系统 db 里的,记录位置 system.profile  ,所以,我们只要查询这个 Collection 的记录就可以获取到我们的 Profile  记录了。列出执行时间长于某一限度(5ms)的 Profile  记录: 

db.system.profile.find( { millis : { $gt : 5 } } ) 

查看最新的 Profile  记录: 

 db.system.profile.find().sort({$natural:-1}).limit(1) 

 { "ts" : ISODate("2012-05-20T16:50:36.321Z"), "info" : "query test.system.profile reslen:1219 

nscanned:8  \nquery: { query: {}, orderby: { $natural: -1.0 } }  nreturned:8 bytes:1203", "millis" : 

0 } 

字段说明 

ts:  该命令在何时执行 

info:  本命令的详细信息 

reslen:  返回结果集的大小 

nscanned:  本次查询扫描的记录数 

nreturned:  本次查询实际返回的结果集 

millis:  该命令执行耗时,以毫秒记  

MongoDB Shell  还提供了一个比较简洁的命令 show profile,可列出最近5 条执行时间超过

1ms的 Profile  记录。