mongodb 索引和查询分析器—dex

默北 mongodb1 17,795字数 3360阅读11分12秒阅读模式

dex 介绍

mongodb索引和查询分析器dex,是一种MongoDB的性能调整工具,比较MongoDB的日志文件和索引条目并给出索引建议。目前,必须提供一个连接数据库的URI。 dex只建议完整的索引,而不是部分索引。不支持Windows平台。

dex 工作原理

dex在运行过程中主要会进行下面三个步骤: 1. 解析query 2. 通过已存在的索引对当前query进行判断 3. 如果发现索引不当,就推荐合适的索引文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

第一步:解析query

Dex会对查询query进行解析,分成下面几大类文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

  • EQUIV – 普通按数值进行的查询,比如:{a: 1}
  • SORT – sort操作,比如: .sort({a: 1})
  • RANGE – 范围查询,比如:Specifically: ‘$ne’, ‘$gt’, ‘$lt’, ‘$gte’, ‘$lte’, ‘$in’, ‘$nin’, ‘$all’, ‘$not’
  • UNSUPPORTED
    • 组合式查询,比如:$and, $or, $nor
    • 除了RANGE之外的嵌套查询

第二步:判断当前索引情况

有两个标准来找出查询所需的索引。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

  • Coverage (none, partial, full) - Coverage表示索引的情况,有括号中的三个值。none表示完全无索引覆盖。full表示query中的字段都能找到索引。partial表示none和full之间的情况。
  • Order (ideal or not) - Order是用于判断索引的顺序是否理想。理想的索引顺序应该是: Equivalence ○ Sort ○ Range 值得注意的是,对地理位置索引只会进行分析,但是不会提出改进建议。

第三步:推荐合适的索引

通过上面两步,我们能够对一个查询可能使用索引的情况有一个了解。Dex会生成一个此查询的最佳索引。如果这个索引不存在,并且查询情况不包括上面提到的UNSUPPORTED,那么Dex就会做出相应的索引优化建议。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

dex 使用

常见用法

指定日志文件和提供必要的验证,如果启用了验证和数据库文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

> dex -f my/mongod/data/path/mongodb.log mongodb://myUser:myPass@myHost:12345/myDb

或是开启db.setProfilingLevel(1),分析后再关闭profiling,db.setProfilingLevel(0)文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

> dex -p mongodb://myUser:myPass@myHost:12345/myDb

通过db或collection过滤

dex支持通过指定特定的db或collection来过滤分析。如果打算分析多个数据库,必须提供一个连接的URI到admin数据库。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

> 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。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

> dex -f my/mongod/data/path/mongodb.log -s 400
> dex -p -n "*.collectionOne" mongodb://myUser:myPass@myHost:12345/admin --slowms 1000

监视模式

通过指定 -w/--watch参数来实时获取当前信息。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

> dex -w -f my/mongod/data/path/mongodb.log mongodb://myUser:myPass@myHost:12345/myDb

当使用-w/--watch和-p/--profile监视system.profile集合时,必须指定单一的库。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

> dex -w -p -n "myDb.*" mongodb://myUser:myPass@myHost:12345/myDb

如果未启用profiling,dex将启用级别1的profiling。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

其他有用的选项

-t/--timeout - Logfile (-f)  针对比较大的日志文件,截取一部分。单位分钟。 --nocheck  忽略现有的索引,对所有的查询进行索引建议。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

环境需求

mongod 2.0.4或以上版本 依赖库有:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

  • pyyaml
  • pymongo
  • dargparse

dex 安装

# 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'.。每个报告包含:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

  • 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.

实例如下

dex文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/ 文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 17/07/2014 01:00:25
  • 转载请务必保留本文链接:https://www.ttlsa.com/mongodb/mongodb-index-and-query-analyzer-tools-dex/
评论  1  访客  0