热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

对MySQL慢查询日志进行分析的基本教程_MySQL

这篇文章主要介绍了对MySQL慢查询日志进行分析的基本教程,文中提到的Query-Digest-UI这个基于BS的图形化查看工具非常好用,需要的朋友可以参考下
0、首先查看当前是否开启慢查询:

(1)快速办法,运行sql语句

show VARIABLES like "%slow%" 

(2)直接去my.conf中查看。

my.conf中的配置(放在[mysqld]下的下方加入)

[mysqld]

log-slow-queries = /usr/local/mysql/var/slowquery.log
long_query_time = 1 #单位是秒
log-queries-not-using-indexes


使用sql语句来修改:不能按照my.conf中的项来修改的。修改通过"show VARIABLES like "%slow%" "
语句列出来的变量,运行如下sql:

set global log_slow_queries = ON;
set global slow_query_log = ON;
set global long_query_time=0.1; #设置大于0.1s的sql语句记录下来

慢查询日志文件的信息格式:

# Time: 130905 14:15:59   时间是2013年9月5日 14:15:59(前面部分容易看错哦,乍看以为是时间戳)
# User@Host: root[root] @ [183.239.28.174] 请求mysql服务器的客户端ip
# Query_time: 0.735883 Lock_time: 0.000078 Rows_sent: 262 Rows_examined: 262 这里表示执行用时多少秒,0.735883秒,1秒等于1000毫秒

SET timestamp=1378361759; 这目前我还不知道干嘛用的
show tables from `test_db`; 这个就是关键信息,指明了当时执行的是这条语句


1、MySQL 慢查询日志分析
pt-query-digest分析慢查询日志

pt-query-digest –report slow.log

报告最近半个小时的慢查询:

pt-query-digest –report –since 1800s slow.log

报告一个时间段的慢查询:

pt-query-digest –report –since ‘2013-02-10 21:48:59′ –until ‘2013-02-16 02:33:50′ slow.log

报告只含select语句的慢查询:

pt-query-digest –filter ‘$event->{fingerprint} =~ m/^select/i' slow.log

报告针对某个用户的慢查询:

pt-query-digest –filter ‘($event->{user} || “”) =~ m/^root/i' slow.log

报告所有的全表扫描或full join的慢查询:

pt-query-digest –filter ‘(($event->{Full_scan} || “”) eq “yes”) || (($event->{Full_join} || “”) eq “yes”)' slow.log


2、将慢查询日志的分析结果可视化
Query-Digest-UI
其实,这是一个非常简单和直接的工具,浏览和统计Mysql慢查询,基于AJAX的Web界面。
配置Query-Digest-UI:

下载:

wget https://nodeload.github.com/kormoc/Query-Digest-UI/zip/master
 unzip Query-Digest-UI-master.zip

查询分析结果可视化步骤如下:

(1)创建相关数据库表

-- install.sql
-- Create the database needed for the Query-Digest-UI
DROP DATABASE IF EXISTS slow_query_log;
CREATE DATABASE slow_query_log;
USE slow_query_log;
 
-- Create the global query review table
CREATE TABLE `global_query_review` (
 `checksum` bigint(20) unsigned NOT NULL,
 `fingerprint` text NOT NULL,
 `sample` longtext NOT NULL,
 `first_seen` datetime DEFAULT NULL,
 `last_seen` datetime DEFAULT NULL,
 `reviewed_by` varchar(20) DEFAULT NULL,
 `reviewed_on` datetime DEFAULT NULL,
 `comments` text,
 `reviewed_status` varchar(24) DEFAULT NULL,
 PRIMARY KEY (`checksum`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
-- Create the historical query review table
CREATE TABLE `global_query_review_history` (
 `hostname_max` varchar(64) NOT NULL,
 `db_max` varchar(64) DEFAULT NULL,
 `checksum` bigint(20) unsigned NOT NULL,
 `sample` longtext NOT NULL,
 `ts_min` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 `ts_max` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 `ts_cnt` float DEFAULT NULL,
 `Query_time_sum` float DEFAULT NULL,
 `Query_time_min` float DEFAULT NULL,
 `Query_time_max` float DEFAULT NULL,
 `Query_time_pct_95` float DEFAULT NULL,
 `Query_time_stddev` float DEFAULT NULL,
 `Query_time_median` float DEFAULT NULL,
 `Lock_time_sum` float DEFAULT NULL,
 `Lock_time_min` float DEFAULT NULL,
 `Lock_time_max` float DEFAULT NULL,
 `Lock_time_pct_95` float DEFAULT NULL,
 `Lock_time_stddev` float DEFAULT NULL,
 `Lock_time_median` float DEFAULT NULL,
 `Rows_sent_sum` float DEFAULT NULL,
 `Rows_sent_min` float DEFAULT NULL,
 `Rows_sent_max` float DEFAULT NULL,
 `Rows_sent_pct_95` float DEFAULT NULL,
 `Rows_sent_stddev` float DEFAULT NULL,
 `Rows_sent_median` float DEFAULT NULL,
 `Rows_examined_sum` float DEFAULT NULL,
 `Rows_examined_min` float DEFAULT NULL,
 `Rows_examined_max` float DEFAULT NULL,
 `Rows_examined_pct_95` float DEFAULT NULL,
 `Rows_examined_stddev` float DEFAULT NULL,
 `Rows_examined_median` float DEFAULT NULL,
 `Rows_affected_sum` float DEFAULT NULL,
 `Rows_affected_min` float DEFAULT NULL,
 `Rows_affected_max` float DEFAULT NULL,
 `Rows_affected_pct_95` float DEFAULT NULL,
 `Rows_affected_stddev` float DEFAULT NULL,
 `Rows_affected_median` float DEFAULT NULL,
 `Rows_read_sum` float DEFAULT NULL,
 `Rows_read_min` float DEFAULT NULL,
 `Rows_read_max` float DEFAULT NULL,
 `Rows_read_pct_95` float DEFAULT NULL,
 `Rows_read_stddev` float DEFAULT NULL,
 `Rows_read_median` float DEFAULT NULL,
 `Merge_passes_sum` float DEFAULT NULL,
 `Merge_passes_min` float DEFAULT NULL,
 `Merge_passes_max` float DEFAULT NULL,
 `Merge_passes_pct_95` float DEFAULT NULL,
 `Merge_passes_stddev` float DEFAULT NULL,
 `Merge_passes_median` float DEFAULT NULL,
 `InnoDB_IO_r_ops_min` float DEFAULT NULL,
 `InnoDB_IO_r_ops_max` float DEFAULT NULL,
 `InnoDB_IO_r_ops_pct_95` float DEFAULT NULL,
 `InnoDB_IO_r_bytes_pct_95` float DEFAULT NULL,
 `InnoDB_IO_r_bytes_stddev` float DEFAULT NULL,
 `InnoDB_IO_r_bytes_median` float DEFAULT NULL,
 `InnoDB_IO_r_wait_min` float DEFAULT NULL,
 `InnoDB_IO_r_wait_max` float DEFAULT NULL,
 `InnoDB_IO_r_wait_pct_95` float DEFAULT NULL,
 `InnoDB_IO_r_ops_stddev` float DEFAULT NULL,
 `InnoDB_IO_r_ops_median` float DEFAULT NULL,
 `InnoDB_IO_r_bytes_min` float DEFAULT NULL,
 `InnoDB_IO_r_bytes_max` float DEFAULT NULL,
 `InnoDB_IO_r_wait_stddev` float DEFAULT NULL,
 `InnoDB_IO_r_wait_median` float DEFAULT NULL,
 `InnoDB_rec_lock_wait_min` float DEFAULT NULL,
 `InnoDB_rec_lock_wait_max` float DEFAULT NULL,
 `InnoDB_rec_lock_wait_pct_95` float DEFAULT NULL,
 `InnoDB_rec_lock_wait_stddev` float DEFAULT NULL,
 `InnoDB_rec_lock_wait_median` float DEFAULT NULL,
 `InnoDB_queue_wait_min` float DEFAULT NULL,
 `InnoDB_queue_wait_max` float DEFAULT NULL,
 `InnoDB_queue_wait_pct_95` float DEFAULT NULL,
 `InnoDB_queue_wait_stddev` float DEFAULT NULL,
 `InnoDB_queue_wait_median` float DEFAULT NULL,
 `InnoDB_pages_distinct_min` float DEFAULT NULL,
 `InnoDB_pages_distinct_max` float DEFAULT NULL,
 `InnoDB_pages_distinct_pct_95` float DEFAULT NULL,
 `InnoDB_pages_distinct_stddev` float DEFAULT NULL,
 `InnoDB_pages_distinct_median` float DEFAULT NULL,
 `QC_Hit_cnt` float DEFAULT NULL,
 `QC_Hit_sum` float DEFAULT NULL,
 `Full_scan_cnt` float DEFAULT NULL,
 `Full_scan_sum` float DEFAULT NULL,
 `Full_join_cnt` float DEFAULT NULL,
 `Full_join_sum` float DEFAULT NULL,
 `Tmp_table_cnt` float DEFAULT NULL,
 `Tmp_table_sum` float DEFAULT NULL,
 `Filesort_cnt` float DEFAULT NULL,
 `Filesort_sum` float DEFAULT NULL,
 `Tmp_table_on_disk_cnt` float DEFAULT NULL,
 `Tmp_table_on_disk_sum` float DEFAULT NULL,
 `Filesort_on_disk_cnt` float DEFAULT NULL,
 `Filesort_on_disk_sum` float DEFAULT NULL,
 `Bytes_sum` float DEFAULT NULL,
 `Bytes_min` float DEFAULT NULL,
 `Bytes_max` float DEFAULT NULL,
 `Bytes_pct_95` float DEFAULT NULL,
 `Bytes_stddev` float DEFAULT NULL,
 `Bytes_median` float DEFAULT NULL,
 UNIQUE KEY `hostname_max` (`hostname_max`,`checksum`,`ts_min`,`ts_max`),
 KEY `ts_min` (`ts_min`),
 KEY `checksum` (`checksum`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(2)创建数据库账号

$ mysql -uroot -p -h 192.168.1.190 

(3)配置Query-Digest-UI

修改数据库连接配置

cd Query-Digest-UI
cp config.php.example config.php
vi config.php
$reviewhost = array(
// Replace hostname and database in this setting
// use host=hostname;port=portnum if not the default port
 'dsn'   => 'mysql:host=192.168.1.190;port=3306;dbname=slow_query_log',
 'user'   => 'slowlog',
 'password'  => '123456',
// See http://www.percona.com/doc/percona-toolkit/2.1/pt-query-digest.html#cmdoption-pt-query-digest--review
 'review_table' => 'global_query_review',
// This table is optional. You don't need it, but you lose detailed stats
// Set to a blank string to disable
// See http://www.percona.com/doc/percona-toolkit/2.1/pt-query-digest.html#cmdoption-pt-query-digest--review-history
 'history_table' => 'global_query_review_history',
);

(4)使用pt-query-digest分析日志并将分析结果导入数据库

pt-query-digest --user=slowlog \
--password=123456 \
--review h=192.168.1.190,D=slow_query_log,t=global_query_review \
--review-history h=192.168.1.190,D=slow_query_log,t=global_query_review_history\
--no-report --limit=0% \
--filter=" \$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$HOSTNAME\"" \
/usr/local/mysql/data/slow.log


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 数据库(外键及其约束理解)(https:www.cnblogs.comchenxiaoheip6909318.html)My ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • yum安装_Redis —yum安装全过程
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Redis—yum安装全过程相关的知识,希望对你有一定的参考价值。访问https://redi ... [详细]
author-avatar
yjlz2012
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有