MySQL性能分析系统2014-03-19对于MySQL慢查询日志的分析,现已由多种工具来提供;最原始的mysqldumpslow,功能比较齐全的 mysqlsla和percona的 pt-query-digest;以上工具大大提高了DBA来分析数据库的性能效率,减少了过多的猜测过程;如果能实现定时分析SQL并且进行可视化展示呢?适用过Query-Digest-UI-master 这个UI插件,在配合 percona的 pt-query-digest工具,只是简单做到一个可视化的结果;如果对于多个服务器的分析,这个表现的就很吃力;对于MySQL慢查询的分析及可视化我们可以利用开源软件Anemometer ,功能:可以针对多个DB中的慢查询进行分析;提供explain 功能;对某个SQL语句的使用情况生成图片,并能生成永久链接;支持performance_schema 性能采集;根据相应时间或其他指标进行排序:等等;对percona pt-query-digest 的使用可以参考这篇博客:http://weipengfei.blog.51cto.com/1511707/953075 (这次使用的版本为2.1)重点说一下下面两个参数:--review:将收集的结果存储到数据库中,下次分析的时候如有重复可不进行记录;--review-history:存储收集的历史数据,并进行SQL趋势分析;(如果没有相应的表的话可 使用 --create-review-table 和 --create-review-history-table 两个参数创建)简单举例:
pt-query-digest --filter "(($event->{Full_scan} || "") eq "yes") || (($event->{Full_join} || "") eq "yes")" slow.log
Anemometer 是需要LNMP或者lamp的支持的(PHP 5.3+);pt-query-digest执行过程:$ pt-query-digest --user=anemometer --password=superSecurePass --review h=db.example.com,D=slow_query_log,t=global_query_review --review-history h=host,D=slow_query_log,t=global_query_review_history
--no-report --limit=0% --filter=" $event->{Bytes} = length($event->{arg}) and $event->{hostname}="$HOSTNAME"" /var/lib/mysql/db.example.com-slow.log对于$HOSTNAME参数我们可以灵活运用,可针对不同主机进行查看;$ mysql -h db.example.com < install.sql配置文件修改:
$ cd anemometer/conf$ cp sample.config.inc.php config.inc.php
$conf["datasources"]["localhost"] = array("host"=> "db.example.com","port"=> 3306,"db"=> "slow_query_log","user"=> "anemometer","password" => "superSecurePass","tables" => array("global_query_review" => "fact","global_query_review_history" => "dimension"));