阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL 5.6使用profile工具分析sql

141次阅读
没有评论

共计 4793 个字符,预计需要花费 12 分钟才能阅读完成。

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              – 显示交换次数相关开销的信息
   
  这里边的可以任意特换就可以看到对应的参数的开销,自己试试看。

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-08/146593.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计4793字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中