show profile是MySQL提供可以用来分析当前会话中SQL语句执行的资源消耗情况。可以用于SQL的调优测量。默认情况下,参数处于关闭状态,并保存最近 15 次的运行结果。
-- 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)
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)
all:显示所有的性能开销信息
后面三个(Creating tmp table、converting HEAP to MyISAM、Copying to tmp table on disk)必须要优化。