Linux ·

MySQL 5.6使用profile工具分析sql

mysql性能这块,其实都是DBA做的事情,一问DBA的朋友就会很快拿出一大堆工具就让你来使用,并且帮助你排查出很多问题。并不是每个公司都有DBA的,很多公司都是开发来弄的,如果业务量特别大并且上来的时候,我们可以招聘一个DBA,一般运维都会帮助来弄。

profile使用工具;

查看自己版本的是否开启了Profile, profiling=on是开启了这个profile功能,
mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.35-log |
+------------+
1 row in set (0.00 sec)
 
mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling        | YES  |
| profiling              | ON    |
| profiling_history_size | 15    |
+------------------------+-------+
3 rows in set (0.01 sec)

Help profile 查看一下,profile的帮助,但是看一下,官方在这个时候会让你注意一下,profile会在5.7以后会废弃了,不用了,所以先查看一下自己的版本,估计好多公司都没用到5.7,还是老的版本。

mysql> help profile;
Name: 'SHOW PROFILE'
Description:
Syntax:
SHOW PROFILE [type [, type] ... ]
    [FOR QUERY n]
    [LIMIT row_count [OFFSET offset]]
 
type: 
    ALL                --显示所有的开销信息 
  | BLOCK IO          --显示块IO相关开销 
  | CONTEXT SWITCHES  --上下文切换相关开销 
  | CPU                --显示CPU相关开销信息 
  | IPC                --显示发送和接收相关开销信息 
  | MEMORY            --显示内存相关开销信息 
  | PAGE FAULTS        --显示页面错误相关开销信息 
  | SOURCE            --显示和Source_function,Source_file,Source_line相关的开销信息 
  | SWAPS              --显示交换次数相关开销的信息
 
The SHOW PROFILE and SHOW PROFILES statements display profiling
information that indicates resource usage for statements executed
during the course of the current session.
 
*Note*: These statements are deprecated as of MySQL 5.6.7 and will be
removed in a future MySQL release. Use the Performance Schema instead;
see
http://dev.mysql.com/doc/refman/5.6/en/performance-schema-query-profili
ng.html.
 
Profiling is controlled by the profiling session variable, which has a

开启一下profiling功能;
 
mysql> set profile=1;
ERROR 1193 (HY000): Unknown system variable 'profile'
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling        | YES  |
| profiling              | ON    |
| profiling_history_size | 15    |
+------------------------+-------+
3 rows in set (0.00 sec)

输入sql语句查看一下,profiles是否产生了。
mysql> SELECT count(*) from table where publishtime >='2017-01-01' and publishtime <='2017-08-30'
    -> ;
+----------+
| count(*) |
+----------+
|  1317564 |
+----------+
1 row in set (2.46 sec)
 
mysql> show profiles;
+----------+------------+--------------------------------------------------------------------------------------------------------+
| Query_ID | Duration  | Query                                                                                                  |
+----------+------------+--------------------------------------------------------------------------------------------------------+
|        1 | 0.00109175 | show variables like '%profil%'                                                                        |
|        2 | 2.46288425 | SELECT count(*) from table where publishtime >='2017-01-01' and publishtime <='2017-08-30' |
+----------+------------+--------------------------------------------------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
 
mysql>
 
获取指定的开销:
mysql> show profile for query 2;
+----------------------+----------+
| Status              | Duration |
+----------------------+----------+
| starting            | 0.000147 |
| checking permissions | 0.000023 |
| Opening tables      | 0.000047 |
| init                | 0.000081 |
| System lock          | 0.000031 |
| optimizing          | 0.000034 |
| statistics          | 0.001650 |
| preparing            | 0.000046 |
| executing            | 0.000018 |
| Sending data        | 2.460588 |
| end                  | 0.000041 |
| query end            | 0.000019 |
| closing tables      | 0.000022 |
| freeing items        | 0.000055 |
| cleaning up          | 0.000085 |
+----------------------+----------+
15 rows in set, 1 warning (0.00 sec)
 
可以看到Sending data ,这个还是比较耗时,这个是sending data 不是发送数据的意思,
是收集和发送集合的数据。
 
查看cpu的数据:
mysql> show profile cpu for query 2;
+----------------------+----------+----------+------------+
| Status              | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting            | 0.000147 | 0.000000 |  0.000000 |
| checking permissions | 0.000023 | 0.000000 |  0.000000 |
| Opening tables      | 0.000047 | 0.000000 |  0.000000 |
| init                | 0.000081 | 0.000000 |  0.000000 |
| System lock          | 0.000031 | 0.000000 |  0.000000 |
| optimizing          | 0.000034 | 0.000000 |  0.000000 |
| statistics          | 0.001650 | 0.001000 |  0.001000 |
| preparing            | 0.000046 | 0.000000 |  0.000000 |
| executing            | 0.000018 | 0.000000 |  0.000000 |
| Sending data        | 2.460588 | 2.464625 |  0.025996 |
| end                  | 0.000041 | 0.000000 |  0.000000 |
| query end            | 0.000019 | 0.000000 |  0.000000 |
| closing tables      | 0.000022 | 0.000000 |  0.000000 |
| freeing items        | 0.000055 | 0.000000 |  0.000000 |
| cleaning up          | 0.000085 | 0.000000 |  0.000000 |
+----------------------+----------+----------+------------+
15 rows in set, 1 warning (0.00 sec)
 
也是这个操作,
 
type: 
    ALL                --显示所有的开销信息 
  | BLOCK IO          --显示块IO相关开销 
  | CONTEXT SWITCHES  --上下文切换相关开销 
  | CPU                --显示CPU相关开销信息 
  | IPC                --显示发送和接收相关开销信息 
  | MEMORY            --显示内存相关开销信息 
  | PAGE FAULTS        --显示页面错误相关开销信息 
  | SOURCE            --显示和Source_function,Source_file,Source_line相关的开销信息 
  | SWAPS              --显示交换次数相关开销的信息
   
  这里边的可以任意特换就可以看到对应的参数的开销,自己试试看。

参与评论