mongodb 索引和查询分析器—dex

原创
2016-06-07 16:37:17 1281浏览

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.?如果发现索引不当,就推荐合适的索引

第一步:解析query

Dex会对查询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
  • 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'.。每个报告包含:
  • 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
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。