mysql 下 show profile 备忘

mysql 中可利用  show profile 进行性能分析

show profile 功能默认状态下为关闭, 因消耗一定资源

 

查询方法

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |  0 = 关闭  1 = 启用
+-------------+
1 row in set (0.00 sec)

打开方法

mysql> set profiling = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

启用 profiling 功能后就可以对 SQL 进行分析。

为避免之前已经把 SQL 存放在 QCACHE 中, 建议在执行 SQL 时, 强制 SELECT 语句不进行 QCACHE 检测

如下面 SQL

mysql> select sql_no_cache distinct prewarebro0_.ID as ID471_, prewarebro0_.SPECIFICATION_ID as SPECIFIC2_471_, prewarebro0_.SPECIES_ID as SPECIES3_471_, prewarebro0_.SPECIES_CODE as SPECIES4_471_,
prewarebro0_.DETAIL_URL as DETAIL5_471_, prewarebro0_.SALEPRICE as SALEPRICE471_, prewarebro0_.TYPE as TYPE471_, prewarebro0_.CREATE_TIME as CREATE8_471_, prewarebro0_.PICTUREURL as PICTUREURL471_, prewarebro0_.USER_KO as USER10_471_ from PRE_WARE_BROSWERS
prewarebro0_ cross join WARE_DIRECTORY_SUMA waredirect1_ cross join WARE_CON_DIECTORY warecondir2_ cross join WARE_MOUNTED waremounte3_ cross join WARE_CATALOG specificat4_ where waredirect1_.ID=warecondir2_.SUMA_ID and warecondir2_.ID=waremounte3_.CON_CODE
and prewarebro0_.SPECIFICATION_ID=waremounte3_.MOUNT_CODE and prewarebro0_.SPECIFICATION_ID=specificat4_.ID and waredirect1_.STATUS='1' and waremounte3_.STATUS='1' and specificat4_.STATUS=1 and prewarebro0_.USER_KO='38a89ba4e0595ff0d06ab0744f64344f' order
by prewarebro0_.CREATE_TIME desc limit 3 ;
Empty set (0.51 sec)

当执行完 SQL 之后,可以对该 SQL 进行分析, 如

mysql> show profile;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000793 |
| checking permissions | 0.000012 |
| checking permissions | 0.000006 |
| checking permissions | 0.000005 |
| checking permissions | 0.000005 |
| checking permissions | 0.000010 |
| Opening tables       | 0.000132 |
| System lock          | 0.000047 |
| init                 | 0.000160 |
| optimizing           | 0.000084 |
| statistics           | 0.000264 |
| preparing            | 0.000146 |
| Creating tmp table   | 0.000100 |
| executing            | 0.000008 |
| Copying to tmp table | 0.515277 |
| Sorting result       | 0.000045 |
| Sending data         | 0.000039 |
| end                  | 0.000012 |
| removing tmp table   | 0.000019 |
| end                  | 0.000008 |
| query end            | 0.000014 |
| closing tables       | 0.000055 |
| freeing items        | 0.000087 |
| logging slow query   | 0.000005 |
| cleaning up          | 0.000014 |
+----------------------+----------+
25 rows in set (0.00 sec)

我们看到了, 明显就是 copying to tmp table 值比较不正常,不进行字面解释了

看看当前我们电脑缓存了多少可以进行分析的 SQL

mysql> show profiles;
+----------+------------+------------------------------------------+
| Query_ID | Duration   | Query                                    |
+----------+------------+------------------------------------------+
|        1 | 0.00026925 | select * from ware_stocks where id < 15  |
|        2 | 0.00032250 | SELECT DATABASE()                        |
|        3 | 0.00102075 | show databases                           |
|        4 | 0.00388575 | show tables                              |
|        5 | 0.00021200 | select * from ware_stocks where id < 15  |
|        6 | 0.01826000 | select * from ware_stocks where id < 55  |
|        7 | 0.02039825 | select * from ware_stocks where id < 155 |
|        8 | 0.03401675 | select * from ware_stocks where id > 3   |
+----------+------------+------------------------------------------+
8 rows in set (0.00 sec)

上面记录了一共 8 条 SQL 分析, 假如要分析某一条, 那么可以执行 (参考上表中 ID 值进行分析)

mysql> show profile for query 8;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000164 |
| Waiting for query cache lock   | 0.000010 |
| checking query cache for query | 0.000197 |
| checking permissions           | 0.000026 |
| Opening tables                 | 0.000094 |
| System lock                    | 0.000038 |
| Waiting for query cache lock   | 0.000095 |
| init                           | 0.000122 |
| optimizing                     | 0.000025 |
| statistics                     | 0.000073 |
| preparing                      | 0.000032 |
| executing                      | 0.000008 |
| Sending data                   | 0.010331 |
| Waiting for query cache lock   | 0.000020 |
| Sending data                   | 0.005885 |
| Waiting for query cache lock   | 0.000011 |
| Sending data                   | 0.005877 |
| Waiting for query cache lock   | 0.000010 |
| Sending data                   | 0.005998 |
| Waiting for query cache lock   | 0.000011 |
| Sending data                   | 0.004589 |
| end                            | 0.000022 |
| query end                      | 0.000031 |
| closing tables                 | 0.000050 |
| freeing items                  | 0.000038 |
| Waiting for query cache lock   | 0.000010 |
| freeing items                  | 0.000213 |
| Waiting for query cache lock   | 0.000009 |
| freeing items                  | 0.000005 |
| storing result in query cache  | 0.000009 |
| logging slow query             | 0.000006 |
| cleaning up                    | 0.000010 |
+--------------------------------+----------+
32 rows in set (0.01 sec)

另外,还可以独立地分析某项资源,方便参考

o ALL displays all information

o BLOCK IO displays counts for block input and output operations

o CONTEXT SWITCHESdisplays counts for voluntary and involuntary
  context switches

o CPU displays user and system CPU usage times

o IPC displays counts for messages sent and received

o MEMORY is not currently implemented

o PAGE FAULTS
displays counts for major and minor page faults

o 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

o SWAPS displays swap counts

参考之前信息,我们看看 Copying to tmp table的IO情况

mysql> show profile block io for query 9;
+----------------------+----------+--------------+---------------+
| Status               | Duration | Block_ops_in | Block_ops_out |
+----------------------+----------+--------------+---------------+
| starting             | 0.000483 |            0 |             0 |
| checking permissions | 0.000012 |            0 |             0 |
| checking permissions | 0.000006 |            0 |             0 |
| checking permissions | 0.000006 |            0 |             0 |
| checking permissions | 0.000006 |            0 |             0 |
| checking permissions | 0.000010 |            0 |             0 |
| Opening tables       | 0.000162 |            0 |             0 |
| System lock          | 0.000056 |            0 |             0 |
| init                 | 0.000179 |            0 |             0 |
| optimizing           | 0.000074 |            0 |             0 |
| statistics           | 0.000225 |            0 |             0 |
| preparing            | 0.000173 |            0 |             0 |
| Creating tmp table   | 0.000127 |            0 |             0 |
| executing            | 0.000008 |            0 |             0 |
| Copying to tmp table | 0.837154 |            0 |             8 | 产生 8 次的 IO 了用于创建 TMP TABLE 了
| Sorting result       | 0.000059 |            0 |             0 |
| Sending data         | 0.000050 |            0 |             0 |
| end                  | 0.000017 |            0 |             0 |
| removing tmp table   | 0.000025 |            0 |             0 |
| end                  | 0.000012 |            0 |             0 |
| query end            | 0.000018 |            0 |             0 |
| closing tables       | 0.000071 |            0 |             0 |
| freeing items        | 0.000097 |            0 |             0 |
| logging slow query   | 0.000006 |            0 |             0 |
| cleaning up          | 0.000012 |            0 |             0 |
+----------------------+----------+--------------+---------------+
25 rows in set (0.00 sec)

参考 CPU

mysql> show profile CPU for query 9;
+----------------------+----------+----------+------------+
| Status               | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting             | 0.000483 | 0.000000 |   0.000000 |
| checking permissions | 0.000012 | 0.000000 |   0.000000 |
| checking permissions | 0.000006 | 0.000000 |   0.000000 |
| checking permissions | 0.000006 | 0.000000 |   0.000000 |
| checking permissions | 0.000006 | 0.000000 |   0.000000 |
| checking permissions | 0.000010 | 0.000000 |   0.000000 |
| Opening tables       | 0.000162 | 0.002000 |   0.000000 |
| System lock          | 0.000056 | 0.000000 |   0.000000 |
| init                 | 0.000179 | 0.000000 |   0.000000 |
| optimizing           | 0.000074 | 0.000000 |   0.000000 |
| statistics           | 0.000225 | 0.000000 |   0.000000 |
| preparing            | 0.000173 | 0.000000 |   0.000000 |
| Creating tmp table   | 0.000127 | 0.000000 |   0.000000 |
| executing            | 0.000008 | 0.000000 |   0.000000 |
| Copying to tmp table | 0.837154 | 1.166823 |   0.001000| 也严重消耗了 CPU
| Sorting result       | 0.000059 | 0.000000 |   0.000000 |
| Sending data         | 0.000050 | 0.000000 |   0.000000 |
| end                  | 0.000017 | 0.000000 |   0.000000 |
| removing tmp table   | 0.000025 | 0.000000 |   0.000000 |
| end                  | 0.000012 | 0.000000 |   0.000000 |
| query end            | 0.000018 | 0.000000 |   0.000000 |
| closing tables       | 0.000071 | 0.000000 |   0.000000 |
| freeing items        | 0.000097 | 0.000000 |   0.000000 |
| logging slow query   | 0.000006 | 0.000000 |   0.000000 |
| cleaning up          | 0.000012 | 0.000000 |   0.000000 |
+----------------------+----------+----------+------------+
25 rows in set (0.01 sec)

总结, 分析后,切记 set profiling = 0 , 另外可以配合  explain 更方便地对执行计划进行分析。

时间: 2024-09-21 00:28:13

mysql 下 show profile 备忘的相关文章

mysql一些操作个人备忘(持续更新)

安装mysql数据库目录 1./usr/local/mysql/bin/mysql_install_db --user=mysql --datadir=/home/mysql/var ##指定安装后的数据目录 mysql 启动/关闭 1./usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/.my.cnf --user=mysql & #启动 2./usr/local/mysql/bin/mysqladmin --default

Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)

js|mysql|oracle|window|数据|数据库  Jsp 环境目前最流行的是 Tomcat5.0.Tomcat5.0 自己包含一个 Web 服务器,如果是测试,就没必要把 Tomcat 与 IIS 或 Apache 集成起来.在 Tomcat 自带的 Web 服务器下可以进行 Jsp 测试.    安装 Tomcat5.0 前需要安装 JDK(如果是 Windows server 2003 就必须安装 JDK,因为 Windows server 2003 是不带 JVM 的-Wind

mysql 数据库导入\导出(总结备忘)

 数据库的基本导入\导出的命令 是 mysqldump 和 source  在linux下直接用命令行操作就可以 在windows下 一般情况下有两种方法一个也是用命令行 另一个是用phpmyadmin   先来说说phpmyadmin 这个工具导出和导入很简单 而且导入时无需建库 直接还原成原来的数据库   用source  也可以还原 但他导入文件时有大小限制不能超过2M 再来说说  mysqldump 和 source  用命令操作很快 但是想把导出的文件再导入时必须先建立一个数据库(这个

CENTOS 6.3下MYSQL主主互备架构配置

MySQL主主互备结构是基于mysql增量日志基础上的,区别于主从复制结构. 在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,这个改变是基于sql语句的改变,如果删除系统数据库源文件或删除后新创建同名MYSQL表实现同步则无效.这样两台服务器互为主从,并且都能向外提供服务,这就比使用主从复制具有更好的性能. 接下来我将使用两个相同的环境来实现这个效果: 系统环境:centos6.3 数据库: mysql-5.6.10 注:在做此项配置之前首先要保证数据库当

MySQL下海量数据的迁移步骤分享_Mysql

公司数据中心计划将海量数据做一次迁移,同时增加某时间字段(原来是datatime类型,现在增加一个date类型),单表数据量达到6亿多条记录,数据是基于时间(月)做的partition由于比较忙,一直没有总结,所以很细节的地方都记不清楚了,此处只是简单总结下当时的情形,备忘 乱打乱撞     最初接到任务,没有明确的入手点,直接就是select * from db limit 10000,动态修改翻页数量,通过控制台看耗时情况,慢 复制代码 代码如下: SELECT IR_SID,IR_HKEY

redhat9安装jdk5、ruby和Erlang备忘

   今天费了一个下午安装了redhat9,并且安装了需要使用的jdk5.netbean.ruby和Erlang.做个备忘. 一.安装jdk5 1.到sun的网站上下载jdk5与netbean5.5的捆绑版本,注意要linux平台的 2.比如下载到/root/目录下,执行 chmod 755 jdk-1_5_0_12-nb-5_5_1-linux-ml.bin 然后执行: ./jdk-1_5_0_12-nb-5_5_1-linux-ml.bin 就会自动启动安装向导,一路选择确定下去就OK了.

Express模版引擎hbs备忘

最近几天折腾了下express,想找个合适的模版引擎,下面是一些折腾过程的备忘 选择标准 选择一门模版语言时,可能会考虑的几点 语法友好(micro tmpl那种语法真是够了) 支持模版嵌套(子模版的概念) 支持模版继承(extend) 前后端共用 有容错处理(最好定位到具体出错位置) 支持预编译(性能好) 注意到hbs,似乎满足大部分的需求:https://github.com/donpark/hbs getting started demo地址:https://github.com/chyi

技术备忘3

   shell单引号与变量 [root@test] a=55 [root@test] echo $a 55 [root@test] echo '$a' $a [root@test] echo ''$a'' #注意此处是两个单引不是一个双引 55 总结: 在单引号中引用变量,需要这样来写(单引号括起来双引号内的变量.) :   '"${a}"' 原文地址 date星期求取 <span style="font-family:Microsoft YaHei">

zabbix 配置管理[备忘]

参见 zabbix-2.2.1 打造 server 端 自定义 rpm 方法 [备忘] 文档, 编译 zabbix 服务器端   另外可选择使用直接编译方法进行 zabbix 软件安装 编译提示: zabbix-2.2.1 下载位置http://downloads.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/2.2.1/zabbix-2.2.1.tar.gz   解压后可选的编译参数 ./configure \ --enable