MySQL Query Profile
MySQL Query Profiler, 可以查询到此 SQL 语句会执行多少, 并看出 CPU/Memory 使用量, 执行过程 System lock, Table lock 花多少时间等等.从启动Profile之后的所有查询包括错误语句都会被记录。mysql的sql语句优化也可以使用explain,但是执行计划是没有办法知道详细的Memory/CPU等使用量。
SHOW PROFILE [type [, type] ... ] [FOR QUERY n] [LIMIT row_count [OFFSET offset]]
type: ALL | BLOCK IO | CONTEXT SWITCHES | CPU | IPC | MEMORY | PAGE FAULTS | SOURCE | SWAPS
Optional type values may be specified to display specific additional types of information:
• ALL displays all information
• BLOCK IO displays counts for block input and output operations
• CONTEXT SWITCHES displays counts for voluntary and involuntary context switches
• CPU displays user and system CPU usage times
• IPC displays counts for messages sent and received
• MEMORY is not currently implemented
• PAGE FAULTS displays counts for major and minor page faults
• SOURCE displays the names of functions from the source code, together with the name and line number of the file in which the function occurs
• SWAPS displays swap counts
默认情况下Mysql的profiling是关闭的,所以首先必须打开profiling
mysql> set profiling='on';
再查看变量:
mysql> show variables like '%profi%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| profiling | ON |
| profiling_history_size | 15 | -- profiling_history_size记录多少次查询
+------------------------+-------+
2 rows in set (0.00 sec)
然后show profiles来查看query ID,然后使用这个query ID进行查询分析。试例如下:
首先也打开了Query Profile,然后执行一条查询语句:
mysql> show profiles;
+----------+------------+-------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------+
| 1 | 0.00122000 | show variables like '%profi%' |
| 2 | 0.65738175 | show processlist |
| 3 | 0.00022550 | SELECT DATABASE() |
| 4 | 1.18196350 | select * from order_item |
+----------+------------+-------------------------------+
4 rows in set (0.59 sec)
mysql> show profile cpu, memory for query 4;
+--------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+--------------------+----------+----------+------------+
| starting | 0.055033 | NULL | NULL |
| Opening tables | 0.967959 | NULL | NULL |
| System lock | 0.000010 | NULL | NULL |
| Table lock | 0.000017 | NULL | NULL |
| init | 0.000121 | NULL | NULL |
| optimizing | 0.000007 | NULL | NULL |
| statistics | 0.000017 | NULL | NULL |
| preparing | 0.000014 | NULL | NULL |
| executing | 0.000006 | NULL | NULL |
| Sending data | 0.158729 | NULL | NULL |
| end | 0.000009 | NULL | NULL |
| query end | 0.000005 | NULL | NULL |
| freeing items | 0.000031 | NULL | NULL |
| logging slow query | 0.000003 | NULL | NULL |
| cleaning up | 0.000005 | NULL | NULL |
+--------------------+----------+----------+------------+
15 rows in set (0.59 sec)
mysql> show profile all for query 4;
+--------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+-----------------+-------------+
| Status | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function | Source_file | Source_line |
+--------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+-----------------+-------------+
| starting | 0.055033 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Opening tables | 0.967959 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_base.cc | 4483 |
| System lock | 0.000010 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\lock.cc | 258 |
| Table lock | 0.000017 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\lock.cc | 269 |
| init | 0.000121 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_select.cc | 2354 |
| optimizing | 0.000007 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_select.cc | 771 |
| statistics | 0.000017 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_select.cc | 953 |
| preparing | 0.000014 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_select.cc | 963 |
| executing | 0.000006 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_select.cc | 1647 |
| Sending data | 0.158729 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_select.cc | 2194 |
| end | 0.000009 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_select.cc | 2399 |
| query end | 0.000005 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_parse.cc | 4822 |
| freeing items | 0.000031 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_parse.cc | 5849 |
| logging slow query | 0.000003 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_parse.cc | 1628 |
| cleaning up | 0.000005 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |NULL | NULL | NULL | NULL | <unknown> | .\sql_parse.cc | 1595 |
+--------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+--------
---------+-------------+
15 rows in set (0.00 sec)
每行都是状态变化的过程以及它们持续的时间。
• QUERY_ID is a numeric statement identifier.
• SEQ is a sequence number indicating the display order for rows with the same QUERY_ID value.
• STATE is the profiling state to which the row measurements apply.
• DURATION indicates how long statement execution remained in the given state, in seconds.
• CPU_USER and CPU_SYSTEM indicate user and system CPU use, in seconds.
• CONTEXT_VOLUNTARY and CONTEXT_INVOLUNTARY indicate how many voluntary and involuntary context switches occurred.
• BLOCK_OPS_IN and BLOCK_OPS_OUT indicate the number of block input and output operations.
• MESSAGES_SENT and MESSAGES_RECEIVED indicate the number of communication messages sent and received.
• PAGE_FAULTS_MAJOR and PAGE_FAULTS_MINOR indicate the number of major and minor page faults.
• SWAPS indicates how many swaps occurred.
• SOURCE_FUNCTION, SOURCE_FILE, and SOURCE_LINE provide information indicating where in the source code the profiled state executes.
Status是关于sql语句的状态,和SHOW FULL PROCESSLIST显示的State一致。关于sql语句状态主要有以下几种:
|
Checking table |
正在检查数据表(这是自动的)。 |
|
Closing tables |
正在将表中修改的数据刷新到磁盘中,同时正在关闭已经用完的表。这是一个很快的操作,如果不是这样的话,就应该确认磁盘空间是否已经满了或者磁盘是否正处于重负中。 |
|
Connect Out |
复制从服务器正在连接主服务器 |
|
Copying to tmp table on disk |
由于临时结果集大于 tmp_table_size ,正在将临时表从内存存储转为磁盘存储以此节省内存 |
|
Creating tmp table |
正在创建临时表以存放部分查询结果 |
|
deleting from main table |
服务器正在执行多表删除中的第一部分,刚删除第一个表。 |
|
deleting from reference tables |
服务器正在执行多表删除中的第二部分,正在删除其他 表的记录。 |
|
Flushing tables |
正在执行 FLUSH TABLES ,等待其他线程关闭数据表 |
|
Killed |
发送了一个 kill 请求给某线程,那么这个线程将会检查 kill 标志位,同时会放弃下一个 kill 请求。 MySQL 会在每次的主循环中检查 kill 标志位,不过有些情况下该线程可能会过一小段才能死掉。如果该线程程被其他线程锁住了,那么 kill 请求会在锁释放时马上生效。 |
|
Locked |
被其他查询锁住了 |
|
Sending data |
正在处理 SELECT 查询的记录,同时正在把结果发送给客户端 |
|
Sorting for group |
正在为 GROUP BY 做排序 |
|
Sorting for order |
正在为 ORDER BY 做排序 |
|
Opening tables |
这个过程应该会很快,除非受到其他因素的干扰。例如,在执 ALTER TABLE 或 LOCK TABLE 语句行完以前,数据表无法被其他线程打开。 正尝试打开一个表 |
|
Removing duplicates |
正在执行一个 SELECT DISTINCT 方式的查询,但是 MySQL 无法在前一个阶段优化掉那些重复的记录。因此, MySQL 需要再次去掉重复的记录,然后再把结果发送给客户端 |
|
Reopen table |
获得了对一个表的锁,但是必须在表结构修改之后才能获得这个锁。已经释放锁,关闭数据表,正尝试重新打开数据表 |
|
Repair by sorting |
修复指令正在排序以创建索引 |
|
Repair with keycache |
修复指令正在利用索引缓存一个一个地创建新索引。它会比 Repair by sorting 慢些 |
|
Searching rows for update |
正在讲符合条件的记录找出来以备更新。它必须在 UPDATE 要修改相关的记录之前就完成了 |
|
Sleeping |
正在等待客户端发送新请求 |
|
System lock |
正在等待取得一个外部的系统锁。如果当前没有运行多个 mysqld 服务器同时请求同一个表,那么可以通过增加 --skip-external-locking 参数来禁止外部系统锁 |
|
Upgrading lock |
INSERT DELAYED 正在尝试取得一个锁表以插入新记录 |
|
Updating |
正在搜索匹配的记录,并且修改它们 |
|
User Lock |
正在等待 GET_LOCK() |
|
Waiting for tables |
该线程得到通知,数据表结构已经被修改了,需要重新打开数据表以取得新的结构。然后,为了能的重新打开数据表,必须等到所有其他线程关闭这个表。以下几种情况下会产生这个通知: FLUSH TABLES tbl_name, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE, 或 OPTIMIZE TABLE |
|
waiting for handler insert |
INSERT DELAYED 已经处理完了所有待处理的插入操作,正在等待新的请求 |
http://dev.mysql.com/doc/refman/5.1/en/show-profile.html
MySQL Query Profile的更多相关文章
- mysql中profile的使用
1.profile在mysql5.0.37及其以上版本中可用 2.查看profile是否可用SELECT @@profiling;来查看是否已经启用profile,如果profilng值为0,可以通过 ...
- 【mysql】Mysql的profile的使用 --- Profilling mysql的性能分析工具
分析SQL执行带来的开销是优化SQL的重要手段. 在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析.该参数可以在全局和session级别来设置.对于全局级别则作用于整个MySQ ...
- Discuz! X3搬家后UCenter出现UCenter info: MySQL Query Error解决方案
Discuz! X3 X2.5论坛搬家后 登录UCenter出现报错:UCenter info: MySQL Query ErrorSQL:SELECT value FROM [Table]vars ...
- MySQL 使用profile分析慢sql,group left join效率高于子查询
MySQL 使用profile分析慢sql,group left join效率高于子查询 http://blog.csdn.net/mchdba/article/details/54380221 -- ...
- Linux下安装MySQL管理工具MySQL Administrator和MySQL Query Browser(转载)
文章来源:http://blog.csdn.net/sunrier/article/details/7572299 Linux下MySQL Administrator和MySQL Query Brow ...
- MySQL Query Cache 相关的问题
最近经常有人问我 MySQL Query Cache 相关的问题,就整理一点 MySQL Query Cache 的内容,以供参考. 顾名思义,MySQL Query Cache 就是用来缓存和 Qu ...
- Tcpdump MySQL Query
在MySQL线上环境我们一般只打开了binary log,slow log,有时我们需要查看general log呢?因为该log记录所有的请求,打开该日志肯定给磁盘造成很大压力,IO能力有所下降,所 ...
- Mysql分析-profile详解
一.前言当我们要对某一条sql的性能进行分析时,可以使用它. Profiling是从 mysql5.0.3版本以后才开放的.启动profile之后,所有查询包括错误的语句都会记录在内.关闭会话或者se ...
- tcpdump 学习(3):MySQL Query
在MySQL线上环境我们一般只打开了binary log,slow log,有时我们需要查看general log呢?因为该log记录所有的请求,打开该日志肯定给磁盘造成很大压力,IO能力有所下降,所 ...
随机推荐
- Apache+PHP+Mysql OS X 10.9 Mavericks WEB 服务器配置
在 OS X 10.9 上基本没有什么特别大的差异. 为了新系统用户方便小弟重新整理了一下,因为在 OSX 10.9 下的 Server 软件进行了不少升级,有些步骤不太一样了. 硬件方面就不在详细描 ...
- SQL Server Profiler:使用方法和指标说明
SQL Server Profiler的中文意思是SQL Server事件探查,一个Sql的监视工具,可以具体到每一行Sql语句,每一次操作,和每一次的连接.感觉这个工具的作用还是很大的,给大家分享一 ...
- 无法打开包括文件:'atlrx.h'的解决办法
VS 2008中由于将ALT项目的部分代码剥离出去成为了独立的开源项目,需要用到ALT中正则表达式等功能就需要手动下载. 我不是第一个遇到这个问题的,所以已经有前人给出了解决方案. 可到http:// ...
- C# MailMessage Attachment 中文名附件发邮件-Firefox中文显示正常,网页打开邮件附件中文名乱码
一.故事 首先通过CDO.Message来获取邮件EML相关数据:邮件标题.邮件内容.邮件附件.发件人.收件人.CC主要就这么几个,其次通过MailMessage来组织邮件通过Python来发送邮件! ...
- Oracle的model语句入门-转
Model语句是Oracle 10g的新功能之一. 本文通过一些简单的例子帮助理解Model语句的用法,复杂使用场景请参考其他文章. 环境:当然需要Oracle 10g以上,本人是在11g上测试的. ...
- 【Android】如何实现ButterKnife
如何实现ButterKnife ButterKnife的原理简述 示例代码ButterKnifeProcedure Pluggable Annotation Processing 注解处理器 Java ...
- 基于nodejs实现js后端化处理
今天D哥给我提了个问题,"用php执行过js没"?咋一听,没戏~~毕竟常规情况下,js是依赖浏览器运行的.想在php后端采集的同时利用js运行结果并传递给php使用,没戏! 然后回 ...
- sublime3+wamp配置php,(无需配环境变量)
思来想去,最后还是决定给自己的手游加简单后端验证.好久没搞php了,最近搜了搜资料,发现现在php比几年前方便简单的多,有wampserver和sublime用.想想当年我还用记事本+phnow呢. ...
- jetty服务器的安装和部署、新增到开机启动服务
Jetty的首页地址是http://www.mortbay.org/jetty/,点击Downloads进入下载介绍页面,由于Jetty7之后,托管服务有Eclipse接替,所以jetty6.1之前( ...
- 一起做RGB-D SLAM 第二季 (二)
本节目标 我们要实现一个基本的文件IO,用于读取TUM数据集中的图像.顺带的,还要做一个参数文件的读取. 设计参数文件读取的类:ParameterReader 首先,我们来做一个参数读取的类.该类读取 ...