show profiles
一、show profile是什么?
show profile是MySQL提供可以用来分析当前会话中SQL语句执行的资源消耗情况。可以用于SQL的调优测量。默认情况下,参数处于关闭状态,并保存最近 15 次的运行结果。
二、查看show profile 是否打开。默认关闭。
-- show variables like 'profiling'; -- 查看show profile s是否打开。
-- set profiling = on 或者 set profiling = 1; -- 打开show profile
mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | OFF |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set profiling =1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | ON |
+---------------+-------+
1 row in set (0.00 sec)
三、运行sql
mysql> select * from film_text limit 10;
+---------+------------------+-----------------------------------------------------------------------------------------------------------------------+
| film_id | title | description |
+---------+------------------+-----------------------------------------------------------------------------------------------------------------------+
| 1 | ACADEMY DINOSAUR | A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies |
| 2 | ACE GOLDFINGER | A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China |
| 3 | ADAPTATION HOLES | A Astounding Reflection of a Lumberjack And a Car who must Sink a Lumberjack in A Baloon Factory |
| 4 | AFFAIR PREJUDICE | A Fanciful Documentary of a Frisbee And a Lumberjack who must Chase a Monkey in A Shark Tank |
| 5 | AFRICAN EGG | A Fast-Paced Documentary of a Pastry Chef And a Dentist who must Pursue a Forensic Psychologist in The Gulf of Mexico |
| 6 | AGENT TRUMAN | A Intrepid Panorama of a Robot And a Boy who must Escape a Sumo Wrestler in Ancient China |
| 7 | AIRPLANE SIERRA | A Touching Saga of a Hunter And a Butler who must Discover a Butler in A Jet Boat |
| 8 | AIRPORT POLLOCK | A Epic Tale of a Moose And a Girl who must Confront a Monkey in Ancient India |
| 9 | ALABAMA DEVIL | A Thoughtful Panorama of a Database Administrator And a Mad Scientist who must Outgun a Mad Scientist in A Jet Boat |
| 10 | ALADDIN CALENDAR | A Action-Packed Tale of a Man And a Lumberjack who must Reach a Feminist in Ancient China |
+---------+------------------+-----------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)
mysql> show profiles; -- 查看所有执行过的sql
+----------+------------+----------------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------------+
| 1 | 0.00053525 | show variables like 'profiling' |
| 2 | 0.00028875 | select * from film_text limit 10 |
+----------+------------+----------------------------------+
2 rows in set (0.00 sec)
分析某条语句:
show profile type1,type2… for query Query_ID
mysql> show profile cpu,block io for query 2;
-- 查询query_id为2的sql执行时的cpu和io资源情况
+--------------------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000044 | 0.000000 | 0.000000 | NULL | NULL |
| Waiting for query cache lock | 0.000003 | 0.000000 | 0.000000 | NULL | NULL |
| checking query cache for query | 0.000005 | 0.000000 | 0.000000 | NULL | NULL |
| checking privileges on cached | 0.000001 | 0.000000 | 0.000000 | NULL | NULL |
| checking permissions | 0.000006 | 0.000000 | 0.000000 | NULL | NULL |
| sending cached result to clien | 0.000227 | 0.000000 | 0.000000 | NULL | NULL |
| logging slow query | 0.000002 | 0.000000 | 0.000000 | NULL | NULL |
| cleaning up | 0.000001 | 0.000000 | 0.000000 | NULL | NULL |
+--------------------------------+----------+----------+------------+--------------+---------------+
8 rows in set (0.00 sec)
show profile返回结果字段含义
- Status :sql 语句执行的状态
- Duration: sql 执行过程中每一个步骤的耗时
- CPU_user: 当前用户占有的 cpu
- CPU_system: 系统占有的 cpu
- Block_ops_in : I/O 输入
- Block_ops_out : I/O 输出
show profile type 选项
all:显示所有的性能开销信息
- block io:显示块 IO 相关的开销信息
- context switches: 上下文切换相关开销
- cpu:显示 CPU 相关的信息
- ipc:显示发送和接收相关的开销信息
- memory:显示内存相关的开销信息
- page faults:显示页面错误相关开销信息
- source:显示和 Source_function、Source_file、Source_line 相关的开销信息
- swaps:显示交换次数的相关信息
tatus出现以下情况的建议
- System lock
确认是由于哪个锁引起的,通常是因为MySQL或InnoDB内核级的锁引起的。建议:如果耗时较大再关注即可,一般情况下都还好 - Sending data
解释:从server端发送数据到客户端,也有可能是接收存储引擎层返回的数据,再发送给客户端,数据量很大时尤其经常能看见。备注:Sending Data不是网络发送,是从硬盘读取,发送到网络是Writing to net。建议:通过索引或加上LIMIT,减少需要扫描并且发送给客户端的数据量 - Sorting result
正在对结果进行排序,类似Creating sort index,不过是正常表,而不是在内存表中进行排序。建议:创建适当的索引 - Table lock
表级锁,没什么好说的,要么是因为MyISAM引擎表级锁,要么是其他情况显式锁表 - create sort index
当前的SELECT中需要用到临时表在进行ORDER BY排序建议:创建适当的索引 - Creating tmp table
创建临时表。先拷贝数据到临时表,用完后再删除临时表。消耗内存,数据来回拷贝删除,消耗时间,建议:优化索引 - converting HEAP to MyISAM
查询结果太大,内存不够,数据往磁盘上搬了。建议:优化索引,可以调整max_heap_table_size - Copying to tmp table on disk
把内存中临时表复制到磁盘上,危险!!!建议:优化索引,可以调整tmp_table_size参数,增大内存临时表大小
后面三个(Creating tmp table、converting HEAP to MyISAM、Copying to tmp table on disk)必须要优化。