dex 介绍 mongodb索引和查询分析器dex,是一种mongodb的性能调整工具,比较mongodb的日志文件和索引条目并给出索引建议。目前,必须提供一个连接数据库的uri。 dex只建议完整的索引,而不是部分索引。不支持windows平台。 dex 工作原理 dex在运行过程中主要
dex 介绍mongodb索引和查询分析器dex,是一种mongodb的性能调整工具,比较mongodb的日志文件和索引条目并给出索引建议。目前,必须提供一个连接数据库的uri。 dex只建议完整的索引,而不是部分索引。不支持windows平台。dex 工作原理dex在运行过程中主要会进行下面三个步骤: 1. 解析query 2.?通过已存在的索引对当前query进行判断 3.?如果发现索引不当,就推荐合适的索引第一步:解析querydex会对查询query进行解析,分成下面几大类
equiv?– 普通按数值进行的查询,比如:{a: 1} sort?– sort操作,比如: .sort({a: 1}) range?– 范围查询,比如:specifically: ‘$ne’, ‘$gt’, ‘$lt’, ‘$gte’, ‘$lte’, ‘$in’, ‘$nin’, ‘$all’, ‘$not’ unsupported组合式查询,比如:$and, $or, $nor 除了range之外的嵌套查询第二步:判断当前索引情况有两个标准来找出查询所需的索引。
coverage (none, partial, full)?- coverage表示索引的情况,有括号中的三个值。none表示完全无索引覆盖。full表示query中的字段都能找到索引。partial表示none和full之间的情况。 order (ideal or not)?- order是用于判断索引的顺序是否理想。理想的索引顺序应该是: equivalence ○ sort ○ range 值得注意的是,对地理位置索引只会进行分析,但是不会提出改进建议。第三步:推荐合适的索引通过上面两步,我们能够对一个查询可能使用索引的情况有一个了解。dex会生成一个此查询的最佳索引。如果这个索引不存在,并且查询情况不包括上面提到的unsupported,那么dex就会做出相应的索引优化建议。
dex 使用常见用法指定日志文件和提供必要的验证,如果启用了验证和数据库
> dex -f my/mongod/data/path/mongodb.log mongodb://myuser:mypass@myhost:12345/mydb
或是开启db.setprofilinglevel(1),分析后再关闭profiling,db.setprofilinglevel(0)
> dex -p mongodb://myuser:mypass@myhost:12345/mydb
通过db或collection过滤dex支持通过指定特定的db或collection来过滤分析。如果打算分析多个数据库,必须提供一个连接的uri到admin数据库。> dex -f my/mongod/data/path/mongodb.log -n myfirstdb.collectionone mongodb://myuser:mypass@myhost:12345/myfirstdb> dex -p -n *.collectionone mongodb://myuser:mypass@myhost:12345/admin> dex -f my/mongod/data/path/mongodb.log -n myfirstdb.* -n myseconddb.* mongodb://myuser:mypass@myhost:12345/admin
通过查询时间(millis)过滤dex还支持通过指定查询执行时间来进行过滤,小于指定时间不分析。-s/--slowms参数来指定时间。单位是millis。> dex -f my/mongod/data/path/mongodb.log -s 400> dex -p -n *.collectionone mongodb://myuser:mypass@myhost:12345/admin --slowms 1000
监视模式通过指定?-w/--watch参数来实时获取当前信息。> dex -w -f my/mongod/data/path/mongodb.log mongodb://myuser:mypass@myhost:12345/mydb
当使用-w/--watch和-p/--profile监视system.profile集合时,必须指定单一的库。> dex -w -p -n mydb.* mongodb://myuser:mypass@myhost:12345/mydb
如果未启用profiling,dex将启用级别1的profiling。其他有用的选项-t/--timeout - logfile (-f) ?针对比较大的日志文件,截取一部分。单位分钟。 --nocheck ?忽略现有的索引,对所有的查询进行索引建议。环境需求mongod 2.0.4或以上版本 依赖库有:pyyaml pymongo dargparsedex 安装# easy_install pip# pip install dex
结果输出说明runstats - 分析日志或profile数据统计段runstats.linesread - 多少条数母(日志或profile)发送到dex. runstats.linesanalyzed -多少条数目dex成功提取查询,并试图给出建议的数量。 runstats.lineswithrecommendations - the number of lines that prompted and could potentially benefit from an index recommendation. runstats.dextime - the time dex was initiated. runstats.logsource - path to logfile processed. null for -p/--profile mode. runstats.timerange - the range of times passed to dex. includes all lines read. runstats.timedout - true if the dex operation times out per the -t/--timeout flag. runstats.timeoutinminutes - if timedout is true, this contains the time. dex provides information and statistics for each unique query in the form of a. a recommendation includes: results - 查询报告,包含给出的索引建议。?标准输出dex 返回的查询报告数组作为结果。每个查询报告是一个唯一的查询,标记为 'querymask'.。每个报告包含:querymask - 查询模式, with values masked ($query for query component, $orderby for sort component) namespace - the mongodb namespace in which to create the index, in the form db.collection stats - specific query statistics aggregated from each query occurrence. stats.count - the total number of queries that occurred. stats.avgtimemillis - the average time this query currently takes. stats.totaltimemillis - the sum amount of time consumed by all of the queries that match the querymask. recommendation - a fully-formed recommendation object.recommendation.index - the index recommended. recommendation.namespace - the recommendation namespace. recommendation.shellcommand - a helpful string for creating the index in the mongodb shell.实例如下 原文地址:mongodb 索引和查询分析器—dex, 感谢原作者分享。