转自:http://www.percona.com/blog/2015/04/16/profiling-mysql-queries-from-performance-schema/

When optimizing queries and investigating performance issues, MySQL comes with built in support for profiling queries aka SET profiling = 1; . This is already awesome and simple to use, but why the PERFORMANCE_SCHEMA alternative?

Because profiling will be removed soon (already deprecated on MySQL 5.6 ad 5.7); the built-in profiling capability can only be enabled per session. This means that you cannot capture profiling information for queries running from other connections. If you are using Percona Server, the profiling option for log_slow_verbosity is a nice alternative, unfortunately, not everyone is using Percona Server.

Now, for a quick demo: I execute a simple query and profile it below. Note that all of these commands are executed from a single session to my test instance.

mysql> SHOW PROFILES;
+----------+------------+----------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------------------+
| 1 | 0.00011150 | SELECT * FROM sysbench.sbtest1 LIMIT 1 |
+----------+------------+----------------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SHOW PROFILE SOURCE FOR QUERY 1;
+----------------------+----------+-----------------------+------------------+-------------+
| Status | Duration | Source_function | Source_file | Source_line |
+----------------------+----------+-----------------------+------------------+-------------+
| starting | 0.000017 | NULL | NULL | NULL |
| checking permissions | 0.000003 | check_access | sql_parse.cc | 5797 |
| Opening tables | 0.000021 | open_tables | sql_base.cc | 5156 |
| init | 0.000009 | mysql_prepare_select | sql_select.cc | 1050 |
| System lock | 0.000005 | mysql_lock_tables | lock.cc | 306 |
| optimizing | 0.000002 | optimize | sql_optimizer.cc | 138 |
| statistics | 0.000006 | optimize | sql_optimizer.cc | 381 |
| preparing | 0.000005 | optimize | sql_optimizer.cc | 504 |
| executing | 0.000001 | exec | sql_executor.cc | 110 |
| Sending data | 0.000025 | exec | sql_executor.cc | 190 |
| end | 0.000002 | mysql_execute_select | sql_select.cc | 1105 |
| query end | 0.000003 | mysql_execute_command | sql_parse.cc | 5465 |
| closing tables | 0.000004 | mysql_execute_command | sql_parse.cc | 5544 |
| freeing items | 0.000005 | mysql_parse | sql_parse.cc | 6969 |
| cleaning up | 0.000006 | dispatch_command | sql_parse.cc | 1874 |
+----------------------+----------+-----------------------+------------------+-------------+
15 rows in set, 1 warning (0.00 sec)

To demonstrate how we can achieve the same with Performance Schema, we first identify our current connection id. In the real world, you might want to get the connection/processlist id of the thread you want to watch i.e. from SHOW PROCESSLIST .

mysql> SELECT THREAD_ID INTO @my_thread_id
-> FROM threads WHERE PROCESSLIST_ID = CONNECTION_ID();
Query OK, 1 row affected (0.00 sec)

Next, we identify the bounding EVENT_IDs for the statement stages. We will look for the statement we wanted to profile using the query below from the events_statements_history_long table. Your LIMIT clause may vary depending on how much queries the server might be getting.

mysql> SELECT THREAD_ID, EVENT_ID, END_EVENT_ID, SQL_TEXT, NESTING_EVENT_ID
-> FROM events_statements_history_long
-> WHERE THREAD_ID = @my_thread_id
-> AND EVENT_NAME = 'statement/sql/select'
-> ORDER BY EVENT_ID DESC LIMIT 3 G
*************************** 1. row ***************************
THREAD_ID: 13848
EVENT_ID: 419
END_EVENT_ID: 434
SQL_TEXT: SELECT THREAD_ID INTO @my_thread_id
FROM threads WHERE PROCESSLIST_ID = CONNECTION_ID()
NESTING_EVENT_ID: NULL
*************************** 2. row ***************************
THREAD_ID: 13848
EVENT_ID: 374
END_EVENT_ID: 392
SQL_TEXT: SELECT * FROM sysbench.sbtest1 LIMIT 1
NESTING_EVENT_ID: NULL
*************************** 3. row ***************************
THREAD_ID: 13848
EVENT_ID: 353
END_EVENT_ID: 364
SQL_TEXT: select @@version_comment limit 1
NESTING_EVENT_ID: NULL
3 rows in set (0.02 sec)

From the results above, we are mostly interested with the EVENT_ID and END_EVENT_ID values from the second row, this will give us the stage events of this particular query from the events_stages_history_long table.

mysql> SELECT EVENT_NAME, SOURCE, (TIMER_END-TIMER_START)/1000000000 as 'DURATION (ms)'
-> FROM events_stages_history_long
-> WHERE THREAD_ID = @my_thread_id AND EVENT_ID BETWEEN 374 AND 392;
+--------------------------------+----------------------+---------------+
| EVENT_NAME | SOURCE | DURATION (ms) |
+--------------------------------+----------------------+---------------+
| stage/sql/init | mysqld.cc:998 | 0.0214 |
| stage/sql/checking permissions | sql_parse.cc:5797 | 0.0023 |
| stage/sql/Opening tables | sql_base.cc:5156 | 0.0205 |
| stage/sql/init | sql_select.cc:1050 | 0.0089 |
| stage/sql/System lock | lock.cc:306 | 0.0047 |
| stage/sql/optimizing | sql_optimizer.cc:138 | 0.0016 |
| stage/sql/statistics | sql_optimizer.cc:381 | 0.0058 |
| stage/sql/preparing | sql_optimizer.cc:504 | 0.0044 |
| stage/sql/executing | sql_executor.cc:110 | 0.0008 |
| stage/sql/Sending data | sql_executor.cc:190 | 0.0251 |
| stage/sql/end | sql_select.cc:1105 | 0.0017 |
| stage/sql/query end | sql_parse.cc:5465 | 0.0031 |
| stage/sql/closing tables | sql_parse.cc:5544 | 0.0037 |
| stage/sql/freeing items | sql_parse.cc:6969 | 0.0056 |
| stage/sql/cleaning up | sql_parse.cc:1874 | 0.0006 |
+--------------------------------+----------------------+---------------+
15 rows in set (0.01 sec)

As you can see the results are pretty close, not exactly the same but close. SHOW PROFILE shows Duration in seconds, while the results above is in milliseconds.

Some limitations to this method though:

  • As we’ve seen it takes a few hoops to dish out the information we need. Because we have to identify the statement we have to profile manually, this procedure may not be easy to port into tools like the sys schema or pstop.
  • Only possible if Performance Schema is enabled (by default its enabled since MySQL 5.6.6, yay!)
  • Does not cover all metrics compared to the native profiling i.e. CONTEXT SWITCHES, BLOCK IO, SWAPS
  • Depending on how busy the server you are running the tests, the sizes of the history tables may be too small, as such you either have to increase or loose the history to early i.e. performance_schema_events_stages_history_long_size variable. Using ps_history might help in this case though with a little modification to the queries.
  • The resulting Duration per event may vary, I would think this may be due to the additional as described on performance_timers table. In any case we hope to get this cleared up as result when this bug is fixed.

Profiling MySQL queries from Performance Schema的更多相关文章

  1. MySQL 5.7 Performance Schema 详解

    refman mysql 5.7 MySQL Performance Schema  用于监视MySQL服务器,且运行时消耗很少的性能.Performance Schema 收集数据库服务器性能参数, ...

  2. [MySQL Reference Manual] 23 Performance Schema结构

    23 MySQL Performance Schema 23 MySQL Performance Schema 23.1 性能框架快速启动 23.2 性能框架配置 23.2.1 性能框架编译时配置 2 ...

  3. Mysql之performance Schema

    Performance schema是用于监控Mysql执行,具有如下特征: 1.用于在运行时探查Mysql Server的执行过程,是由Performance_schema引擎和 Performan ...

  4. MySQL调优性能监控之performance schema

    一.performance_schema的介绍 performance:性能 schema:图(表)示,以大纲或模型的形式表示计划或理论. MySQL的performance schema 用于监控M ...

  5. MySQL Performance Schema详解

    MySQL的performance schema 用于监控MySQL server在一个较低级别的运行过程中的资源消耗.资源等待等情况. 1 performance schema特点 提供了一种在数据 ...

  6. 通过performance schema收集慢查询

    MySQL5.6起performance schema自动开启,里面涉及记录 statement event的表 mysql> show tables like '%statement%'; + ...

  7. mysql performance schema的即时诊断工具-邱伟胜

    https://github.com/noodba http://www.noodba.com

  8. 【MySQL】MySQL 5.7 sys Schema

    sys库说明:http://dev.mysql.com/doc/refman/5.7/en/sys-schema-usage.html sys库使用说明:http://dev.mysql.com/do ...

  9. MySql(九):MySQL性能调优——Schema设计的性能优化

    一.高效的模型设计 先了解下数据库设计的三大范式 第一范式:要求有主键,并且要求每一个字段原子性不可再分 第二范式:要求所有非主键字段完全依赖主键,不能产生部分依赖 第三范式:所有非主键字段和主键字段 ...

随机推荐

  1. 《深入理解Java集合框架》系列文章

    Introduction 关于C++标准模板库(Standard Template Library, STL)的书籍和资料有很多,关于Java集合框架(Java Collections Framewo ...

  2. javascript之纯数字验证

    现在有一个需求如下图: 产品经理说Card Number只能让输入数字(中间的空格是格式自加的,也是用js实现的),有时候我脑海中出现了个声音,啥玩意,加个type=number不就行了,事实发现图样 ...

  3. UnityShader快速上手指南(二)

    简介 前一篇介绍了如果编写最基本的shader,接下来本文将会简单的深入一下,我们先来看下效果吧 呃,gif效果不好,实际效果是很平滑的动态过渡 实现思路 1.首先我们要实现一个彩色方块 2.让色彩动 ...

  4. html5人物图片360度立体旋转

    体验效果:http://hovertree.com/texiao/html5/10.htm 下载:http://hovertree.com/hvtart/bjae/t16oddyt.htm 代码如下: ...

  5. 实例对比剖析c#引用参数的用法

    c#引用参数传递的深入剖析值类型的变量存储数据,而引用类型的变量存储对实际数据的引用.(这一点很重要,明白了之后就能区分开值类型和引用类型的差别) 在参数传递时,值类型是以值的形式传递的(传递的是值, ...

  6. ActiveReports 报表应用教程 (4)---分栏报表

    在 ActiveReports 中可以实现分栏报表布局样式,可以设置横向分栏.纵向分栏,同时进行分栏和分组设置,统计分栏分组的小计.合计等.在商业报表系统中常见的分栏报表有商品标签.员工工卡.条码打印 ...

  7. 使用layout_weight设置控件占屏幕百分比

    水平LinearLayout中如果A,B两个控件都是layout_weight="1",那么控件在水平方向占比为A的layout_width+1/2空闲空间,B的layout_wi ...

  8. 泛函编程(20)-泛函库设计-Further Into Parallelism

    上两节我们建了一个并行运算组件库,实现了一些基本的并行运算功能.到现在这个阶段,编写并行运算函数已经可以和数学代数解题相近了:我们了解了问题需求,然后从类型匹配入手逐步产生题解.下面我们再多做几个练习 ...

  9. 基于 ANSIBLE 自动化运维实践

    摘要:运维这个话题很痛苦,你做任何的产品都离不开运维.不管你用什么语言.什么平台.什么技术,真正能够决定你产品成熟度的很有可能就是你运维的能力.取自 云巴 CEO 张虎在 ECUG 大会上的分享. 云 ...

  10. ComboBox的联动(三层架构)

    需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值 我们用三层架构的模式来实现 1.我们想和数据库交互,我们首先得来先解决DAL数据库交互层 01.获得年级下拉框的数据 在GradeDAL ...