查看mysql正在执行的SQL语句,使用profile分析SQL执行状态
http://qq85609655.iteye.com/blog/2113960
1)我们先通过status命令查看Mysql运行状态
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
Connection id: 113752
Current database: information_schema
Current user: push_user@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.1.73 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: utf8
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /tmp/mysql.sock
Uptime: 22 days 8 hours 31 min 23 sec
Threads: 38 Questions: 1037751897 Slow queries: 2356 Opens: 79836 Flush tables: 1 Open tables: 64 Queries per second avg: 537.282
--------------
在上面显示列表的最后一条,我们来查看Slow queries这一项的值,如果多次查看的值大于0的话,说明有些查询sql命令执行时间过长。
2)这时再通过show processlist命令来查看当前正在运行的SQL,从中找出运行慢的SQL语句,找到执行慢的语句后,再用explain命令查看这些语句的执行计划。
mysql> show processlist;
+--------+-----------+---------------------+--------------------+---------+-------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+-----------+---------------------+--------------------+---------+-------+-------+------------------+
| 50270 | ambari | DataBase-01:41512 | ambari | Sleep | 23 | | NULL |
| 50271 | ambari | DataBase-01:41511 | ambari | Sleep | 6 | | NULL |
| 50272 | ambari | DataBase-01:41514 | ambari | Sleep | 23 | | NULL |
| 62452 | oozie | DataBase-02:42987 | oozie | Sleep | 25 | | NULL |
| 63660 | ambari | DataBase-01:56052 | ambari | Sleep | 0 | | NULL |
| 110404 | push_user | localhost:33817 | quartz | Sleep | 12 | | NULL |
| 112835 | push_user | localhost:46571 | hibernate | Sleep | 1 | | NULL |
| 113163 | push_user | localhost:56585 | hibernate | Sleep | 1 | | NULL |
| 113289 | push_user | 14.118.132.20:47333 | DW | Sleep | 628 | | NULL |
| 113320 | push_user | localhost:47428 | hibernate | Sleep | 3 | | NULL |
| 113321 | push_user | localhost:47429 | hibernate | Sleep | 3 | | NULL |
| 113322 | push_user | localhost:47430 | hibernate | Sleep | 3 | | NULL |
| 113357 | push_user | localhost:52337 | hibernate | Sleep | 3 | | NULL |
| 113364 | push_user | localhost:57206 | hibernate | Sleep | 3 | | NULL |
| 113366 | push_user | localhost:34813 | hibernate | Sleep | 1 | | NULL |
| 113398 | push_user | localhost:37382 | hibernate | Sleep | 1 | | NULL |
| 113498 | push_user | localhost:47626 | quartz | Sleep | 12717 | | NULL |
| 113709 | push_user | localhost:59382 | hibernate | Sleep | 1 | | NULL |
| 113710 | push_user | localhost:33627 | hibernate | Sleep | 1 | | NULL |
| 113715 | hive | DataBase-02:54968 | hive | Sleep | 2390 | | NULL |
| 113716 | hive | DataBase-02:54969 | hive | Sleep | 2390 | | NULL |
| 113717 | hive | DataBase-02:54974 | hive | Sleep | 2336 | | NULL |
| 113718 | hive | DataBase-02:54975 | hive | Sleep | 2336 | | NULL |
| 113719 | push_user | localhost:48243 | hibernate | Sleep | 1 | | NULL |
| 113720 | push_user | localhost:48245 | hibernate | Sleep | 1 | | NULL |
| 113721 | push_user | localhost:48244 | hibernate | Sleep | 1 | | NULL |
| 113722 | push_user | localhost:48247 | hibernate | Sleep | 1 | | NULL |
| 113723 | push_user | localhost:48249 | hibernate | Sleep | 1 | | NULL |
| 113724 | push_user | localhost:48248 | hibernate | Sleep | 1 | | NULL |
| 113745 | push_user | localhost:50684 | hibernate | Sleep | 1 | | NULL |
| 113746 | push_user | localhost:50685 | hibernate | Sleep | 1 | | NULL |
| 113747 | push_user | localhost:50695 | hibernate | Sleep | 1 | | NULL |
| 113748 | push_user | localhost:50696 | hibernate | Sleep | 1 | | NULL |
| 113749 | push_user | localhost:50697 | hibernate | Sleep | 1 | | NULL |
| 113750 | push_user | localhost:50699 | hibernate | Sleep | 1 | | NULL |
| 113751 | push_user | localhost:50700 | hibernate | Sleep | 1 | | NULL |
| 113752 | push_user | localhost | information_schema | Query | 0 | NULL | show processlist |
| 113753 | push_user | 14.118.132.20:28688 | DW | Sleep | 396 | | NULL |
+--------+-----------+---------------------+--------------------+---------+-------+-------+------------------+
38 rows in set (0.00 sec)
或者通过如下命令查询:
mysql> use information_schema;
mysql> select * from PROCESSLIST where info is not null;
+--------+-----------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+--------+-----------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
| 113752 | push_user | localhost | information_schema | Query | 0 | executing | select * from PROCESSLIST where info is not null |
+--------+-----------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+
1 row in set (0.00 sec)
MySQL使用profile分析SQL执行状态
打开profile
- mysql> select @@have_profiling;
- +------------------+
- | @@have_profiling |
- +------------------+
- | YES |
- +------------------+
- 1 row in set (0.00 sec)
- mysql> select @@profiling;
- +-------------+
- | @@profiling |
- +-------------+
- | 0 |
- +-------------+
- 1 row in set (0.00 sec)
- mysql> set session profiling=1;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select @@profiling;
- +-------------+
- | @@profiling |
- +-------------+
- | 1 |
- +-------------+
- 1 row in set (0.00 sec)
- mysql> select @@have_profiling;
- +------------------+
- | @@have_profiling |
- +------------------+
- | YES |
- +------------------+
- 1 row in set (0.00 sec)
- mysql> select @@profiling;
- +-------------+
- | @@profiling |
- +-------------+
- | 0 |
- +-------------+
- 1 row in set (0.00 sec)
- mysql> set session profiling=1;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select @@profiling;
- +-------------+
- | @@profiling |
- +-------------+
- | 1 |
- +-------------+
- 1 row in set (0.00 sec)
mysql> select @@have_profiling;
+------------------+
| @@have_profiling |
+------------------+
| YES |
+------------------+
1 row in set (0.00 sec) mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec) mysql> set session profiling=1;
Query OK, 0 rows affected (0.00 sec) mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
使用profile分析SQL,可以看到执行两次后,Send data和sending cached result to clien执行效率的变化
- mysql> select count(*) from sakila.payment;
- +----------+
- | count(*) |
- +----------+
- | 16049 |
- +----------+
- 1 row in set (0.03 sec)
- mysql> show profiles;
- +----------+------------+-------------------------------------+
- | Query_ID | Duration | Query |
- +----------+------------+-------------------------------------+
- | 1 | 0.00020400 | select @@profiling |
- | 2 | 0.00008900 | select count(*) from payment |
- | 3 | 0.00006800 | show databaes |
- | 4 | 0.02102800 | show databases |
- | 5 | 0.02847600 | select count(*) from sakila.payment |
- +----------+------------+-------------------------------------+
- 5 rows in set (0.00 sec)
- mysql> select count(*) from sakila.payment;
- +----------+
- | count(*) |
- +----------+
- | 16049 |
- +----------+
- 1 row in set (0.03 sec)
- mysql> show profiles;
- +----------+------------+-------------------------------------+
- | Query_ID | Duration | Query |
- +----------+------------+-------------------------------------+
- | 1 | 0.00020400 | select @@profiling |
- | 2 | 0.00008900 | select count(*) from payment |
- | 3 | 0.00006800 | show databaes |
- | 4 | 0.02102800 | show databases |
- | 5 | 0.02847600 | select count(*) from sakila.payment |
- +----------+------------+-------------------------------------+
- 5 rows in set (0.00 sec)
mysql> select count(*) from sakila.payment;
+----------+
| count(*) |
+----------+
| 16049 |
+----------+
1 row in set (0.03 sec) mysql> show profiles;
+----------+------------+-------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------+
| 1 | 0.00020400 | select @@profiling |
| 2 | 0.00008900 | select count() from payment |
| 3 | 0.00006800 | show databaes |
| 4 | 0.02102800 | show databases |
| 5 | 0.02847600 | select count() from sakila.payment |
+----------+------------+-------------------------------------+
5 rows in set (0.00 sec)
- mysql> show profile for query 5;
- +--------------------------------+----------+
- | Status | Duration |
- +--------------------------------+----------+
- | starting | 0.000030 |
- | Waiting for query cache lock | 0.000005 |
- | checking query cache for query | 0.000043 |
- | checking permissions | 0.000007 |
- | Opening tables | 0.000027 |
- | System lock | 0.000010 |
- | Waiting for query cache lock | 0.000010 |
- | init | 0.000000 |
- | optimizing | 0.023255 |
- | statistics | 0.000118 |
- | preparing | 0.000041 |
- | executing | 0.000033 |
- | Sending data | 0.003833 |
- | end | 0.000054 |
- | query end | 0.000045 |
- | closing tables | 0.000045 |
- | freeing items | 0.000072 |
- | Waiting for query cache lock | 0.000033 |
- | freeing items | 0.000785 |
- | Waiting for query cache lock | 0.000016 |
- | freeing items | 0.000002 |
- | storing result in query cache | 0.000005 |
- | logging slow query | 0.000003 |
- | cleaning up | 0.000004 |
- +--------------------------------+----------+
- 24 rows in set (0.00 sec)
- mysql> select count(*) from sakila.payment;
- +----------+
- | count(*) |
- +----------+
- | 16049 |
- +----------+
- 1 row in set (0.00 sec)
- mysql> show profiles;
- +----------+------------+-------------------------------------+
- | Query_ID | Duration | Query |
- +----------+------------+-------------------------------------+
- | 1 | 0.00020400 | select @@profiling |
- | 2 | 0.00008900 | select count(*) from payment |
- | 3 | 0.00006800 | show databaes |
- | 4 | 0.02102800 | show databases |
- | 5 | 0.02847600 | select count(*) from sakila.payment |
- | 6 | 0.00006900 | select count(*) from sakila.payment |
- +----------+------------+-------------------------------------+
- 6 rows in set (0.00 sec)
- mysql> show profile for query 6;
- +--------------------------------+----------+
- | Status | Duration |
- +--------------------------------+----------+
- | starting | 0.000029 |
- | Waiting for query cache lock | 0.000004 |
- | checking query cache for query | 0.000007 |
- | checking privileges on cached | 0.000004 |
- | checking permissions | 0.000008 |
- | sending cached result to clien | 0.000012 |
- | logging slow query | 0.000002 |
- | cleaning up | 0.000003 |
- +--------------------------------+----------+
- 8 rows in set (0.00 sec)
- mysql> show profile for query 5;
- +--------------------------------+----------+
- | Status | Duration |
- +--------------------------------+----------+
- | starting | 0.000030 |
- | Waiting for query cache lock | 0.000005 |
- | checking query cache for query | 0.000043 |
- | checking permissions | 0.000007 |
- | Opening tables | 0.000027 |
- | System lock | 0.000010 |
- | Waiting for query cache lock | 0.000010 |
- | init | 0.000000 |
- | optimizing | 0.023255 |
- | statistics | 0.000118 |
- | preparing | 0.000041 |
- | executing | 0.000033 |
- | Sending data | 0.003833 |
- | end | 0.000054 |
- | query end | 0.000045 |
- | closing tables | 0.000045 |
- | freeing items | 0.000072 |
- | Waiting for query cache lock | 0.000033 |
- | freeing items | 0.000785 |
- | Waiting for query cache lock | 0.000016 |
- | freeing items | 0.000002 |
- | storing result in query cache | 0.000005 |
- | logging slow query | 0.000003 |
- | cleaning up | 0.000004 |
- +--------------------------------+----------+
- 24 rows in set (0.00 sec)
- mysql> select count(*) from sakila.payment;
- +----------+
- | count(*) |
- +----------+
- | 16049 |
- +----------+
- 1 row in set (0.00 sec)
- mysql> show profiles;
- +----------+------------+-------------------------------------+
- | Query_ID | Duration | Query |
- +----------+------------+-------------------------------------+
- | 1 | 0.00020400 | select @@profiling |
- | 2 | 0.00008900 | select count(*) from payment |
- | 3 | 0.00006800 | show databaes |
- | 4 | 0.02102800 | show databases |
- | 5 | 0.02847600 | select count(*) from sakila.payment |
- | 6 | 0.00006900 | select count(*) from sakila.payment |
- +----------+------------+-------------------------------------+
- 6 rows in set (0.00 sec)
- mysql> show profile for query 6;
- +--------------------------------+----------+
- | Status | Duration |
- +--------------------------------+----------+
- | starting | 0.000029 |
- | Waiting for query cache lock | 0.000004 |
- | checking query cache for query | 0.000007 |
- | checking privileges on cached | 0.000004 |
- | checking permissions | 0.000008 |
- | sending cached result to clien | 0.000012 |
- | logging slow query | 0.000002 |
- | cleaning up | 0.000003 |
- +--------------------------------+----------+
- 8 rows in set (0.00 sec)
mysql> show profile for query 5;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000030 |
| Waiting for query cache lock | 0.000005 |
| checking query cache for query | 0.000043 |
| checking permissions | 0.000007 |
| Opening tables | 0.000027 |
| System lock | 0.000010 |
| Waiting for query cache lock | 0.000010 |
| init | 0.000000 |
| optimizing | 0.023255 |
| statistics | 0.000118 |
| preparing | 0.000041 |
| executing | 0.000033 |
| Sending data | 0.003833 |
| end | 0.000054 |
| query end | 0.000045 |
| closing tables | 0.000045 |
| freeing items | 0.000072 |
| Waiting for query cache lock | 0.000033 |
| freeing items | 0.000785 |
| Waiting for query cache lock | 0.000016 |
| freeing items | 0.000002 |
| storing result in query cache | 0.000005 |
| logging slow query | 0.000003 |
| cleaning up | 0.000004 |
+--------------------------------+----------+
24 rows in set (0.00 sec) mysql> select count() from sakila.payment;
+----------+
| count() |
+----------+
| 16049 |
+----------+
1 row in set (0.00 sec) mysql> show profiles;
+----------+------------+-------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------+
| 1 | 0.00020400 | select @@profiling |
| 2 | 0.00008900 | select count() from payment |
| 3 | 0.00006800 | show databaes |
| 4 | 0.02102800 | show databases |
| 5 | 0.02847600 | select count() from sakila.payment |
| 6 | 0.00006900 | select count(*) from sakila.payment |
+----------+------------+-------------------------------------+
6 rows in set (0.00 sec) mysql> show profile for query 6;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000029 |
| Waiting for query cache lock | 0.000004 |
| checking query cache for query | 0.000007 |
| checking privileges on cached | 0.000004 |
| checking permissions | 0.000008 |
| sending cached result to clien | 0.000012 |
| logging slow query | 0.000002 |
| cleaning up | 0.000003 |
+--------------------------------+----------+
8 rows in set (0.00 sec)
sending data比较耗费cpu
- mysql> select min(seq) seq,state,count(*) numb_ops,
- -> round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur,
- -> round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu
- -> from information_schema.profiling
- -> where query_id = 5
- -> group by state
- -> order by seq;
- +------+--------------------------------+----------+---------+---------+---------+---------+
- | seq | state | numb_ops | sum_dur | avg_dur | sum_cpu | avg_cpu |
- +------+--------------------------------+----------+---------+---------+---------+---------+
- | 2 | starting | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
- | 3 | Waiting for query cache lock | 4 | 0.00006 | 0.00002 | 0.00100 | 0.00025 |
- | 4 | checking query cache for query | 1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |
- | 5 | checking permissions | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
- | 6 | Opening tables | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
- | 7 | System lock | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
- | 9 | init | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
- | 10 | optimizing | 1 | 0.02326 | 0.02326 | 0.00000 | 0.00000 |
- | 11 | statistics | 1 | 0.00012 | 0.00012 | 0.00000 | 0.00000 |
- | 12 | preparing | 1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |
- | 13 | executing | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
- | 14 | Sending data | 1 | 0.00383 | 0.00383 | 0.00300 | 0.00300 |
- | 15 | end | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
- | 16 | query end | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
- | 17 | closing tables | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
- | 18 | freeing items | 3 | 0.00086 | 0.00029 | 0.00000 | 0.00000 |
- | 23 | storing result in query cache | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
- | 24 | logging slow query | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
- | 25 | cleaning up | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
- +------+--------------------------------+----------+---------+---------+---------+---------+
- 19 rows in set (0.00 sec)
- mysql> show profile cpu for query 5;
- +--------------------------------+----------+----------+------------+
- | Status | Duration | CPU_user | CPU_system |
- +--------------------------------+----------+----------+------------+
- | starting | 0.000030 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000005 | 0.000000 | 0.000000 |
- | checking query cache for query | 0.000043 | 0.000000 | 0.000000 |
- | checking permissions | 0.000007 | 0.000000 | 0.000000 |
- | Opening tables | 0.000027 | 0.000000 | 0.000000 |
- | System lock | 0.000010 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000010 | 0.001000 | 0.000000 |
- | init | 0.000000 | 0.000000 | 0.000000 |
- | optimizing | 0.023255 | 0.000000 | 0.000000 |
- | statistics | 0.000118 | 0.000000 | 0.000000 |
- | preparing | 0.000041 | 0.000000 | 0.000000 |
- | executing | 0.000033 | 0.000000 | 0.000000 |
- | Sending data | 0.003833 | 0.002999 | 0.001000 |
- | end | 0.000054 | 0.000000 | 0.000000 |
- | query end | 0.000045 | 0.000000 | 0.000000 |
- | closing tables | 0.000045 | 0.000000 | 0.000000 |
- | freeing items | 0.000072 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000033 | 0.000000 | 0.000000 |
- | freeing items | 0.000785 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000016 | 0.000000 | 0.000000 |
- | freeing items | 0.000002 | 0.000000 | 0.000000 |
- | storing result in query cache | 0.000005 | 0.000000 | 0.000000 |
- | logging slow query | 0.000003 | 0.000000 | 0.000000 |
- | cleaning up | 0.000004 | 0.000000 | 0.000000 |
- +--------------------------------+----------+----------+------------+
- 24 rows in set (0.00 sec)
- mysql> show profile block io for query 5;
- +--------------------------------+----------+--------------+---------------+
- | Status | Duration | Block_ops_in | Block_ops_out |
- +--------------------------------+----------+--------------+---------------+
- | starting | 0.000030 | 0 | 0 |
- | Waiting for query cache lock | 0.000005 | 0 | 0 |
- | checking query cache for query | 0.000043 | 0 | 0 |
- | checking permissions | 0.000007 | 0 | 0 |
- | Opening tables | 0.000027 | 0 | 0 |
- | System lock | 0.000010 | 0 | 0 |
- | Waiting for query cache lock | 0.000010 | 0 | 0 |
- | init | 0.000000 | 0 | 0 |
- | optimizing | 0.023255 | 0 | 0 |
- | statistics | 0.000118 | 0 | 0 |
- | preparing | 0.000041 | 0 | 0 |
- | executing | 0.000033 | 0 | 0 |
- | Sending data | 0.003833 | 0 | 0 |
- | end | 0.000054 | 0 | 0 |
- | query end | 0.000045 | 0 | 0 |
- | closing tables | 0.000045 | 0 | 0 |
- | freeing items | 0.000072 | 0 | 0 |
- | Waiting for query cache lock | 0.000033 | 0 | 0 |
- | freeing items | 0.000785 | 0 | 0 |
- | Waiting for query cache lock | 0.000016 | 0 | 0 |
- | freeing items | 0.000002 | 0 | 0 |
- | storing result in query cache | 0.000005 | 0 | 0 |
- | logging slow query | 0.000003 | 0 | 0 |
- | cleaning up | 0.000004 | 0 | 0 |
- +--------------------------------+----------+--------------+---------------+
- 24 rows in set (0.00 sec)
- mysql> select min(seq) seq,state,count(*) numb_ops,
- -> round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur,
- -> round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu
- -> from information_schema.profiling
- -> where query_id = 5
- -> group by state
- -> order by seq;
- +------+--------------------------------+----------+---------+---------+---------+---------+
- | seq | state | numb_ops | sum_dur | avg_dur | sum_cpu | avg_cpu |
- +------+--------------------------------+----------+---------+---------+---------+---------+
- | 2 | starting | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
- | 3 | Waiting for query cache lock | 4 | 0.00006 | 0.00002 | 0.00100 | 0.00025 |
- | 4 | checking query cache for query | 1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |
- | 5 | checking permissions | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
- | 6 | Opening tables | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
- | 7 | System lock | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
- | 9 | init | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
- | 10 | optimizing | 1 | 0.02326 | 0.02326 | 0.00000 | 0.00000 |
- | 11 | statistics | 1 | 0.00012 | 0.00012 | 0.00000 | 0.00000 |
- | 12 | preparing | 1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |
- | 13 | executing | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
- | 14 | Sending data | 1 | 0.00383 | 0.00383 | 0.00300 | 0.00300 |
- | 15 | end | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
- | 16 | query end | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
- | 17 | closing tables | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
- | 18 | freeing items | 3 | 0.00086 | 0.00029 | 0.00000 | 0.00000 |
- | 23 | storing result in query cache | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
- | 24 | logging slow query | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
- | 25 | cleaning up | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
- +------+--------------------------------+----------+---------+---------+---------+---------+
- 19 rows in set (0.00 sec)
- mysql> show profile cpu for query 5;
- +--------------------------------+----------+----------+------------+
- | Status | Duration | CPU_user | CPU_system |
- +--------------------------------+----------+----------+------------+
- | starting | 0.000030 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000005 | 0.000000 | 0.000000 |
- | checking query cache for query | 0.000043 | 0.000000 | 0.000000 |
- | checking permissions | 0.000007 | 0.000000 | 0.000000 |
- | Opening tables | 0.000027 | 0.000000 | 0.000000 |
- | System lock | 0.000010 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000010 | 0.001000 | 0.000000 |
- | init | 0.000000 | 0.000000 | 0.000000 |
- | optimizing | 0.023255 | 0.000000 | 0.000000 |
- | statistics | 0.000118 | 0.000000 | 0.000000 |
- | preparing | 0.000041 | 0.000000 | 0.000000 |
- | executing | 0.000033 | 0.000000 | 0.000000 |
- | Sending data | 0.003833 | 0.002999 | 0.001000 |
- | end | 0.000054 | 0.000000 | 0.000000 |
- | query end | 0.000045 | 0.000000 | 0.000000 |
- | closing tables | 0.000045 | 0.000000 | 0.000000 |
- | freeing items | 0.000072 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000033 | 0.000000 | 0.000000 |
- | freeing items | 0.000785 | 0.000000 | 0.000000 |
- | Waiting for query cache lock | 0.000016 | 0.000000 | 0.000000 |
- | freeing items | 0.000002 | 0.000000 | 0.000000 |
- | storing result in query cache | 0.000005 | 0.000000 | 0.000000 |
- | logging slow query | 0.000003 | 0.000000 | 0.000000 |
- | cleaning up | 0.000004 | 0.000000 | 0.000000 |
- +--------------------------------+----------+----------+------------+
- 24 rows in set (0.00 sec)
- mysql> show profile block io for query 5;
- +--------------------------------+----------+--------------+---------------+
- | Status | Duration | Block_ops_in | Block_ops_out |
- +--------------------------------+----------+--------------+---------------+
- | starting | 0.000030 | 0 | 0 |
- | Waiting for query cache lock | 0.000005 | 0 | 0 |
- | checking query cache for query | 0.000043 | 0 | 0 |
- | checking permissions | 0.000007 | 0 | 0 |
- | Opening tables | 0.000027 | 0 | 0 |
- | System lock | 0.000010 | 0 | 0 |
- | Waiting for query cache lock | 0.000010 | 0 | 0 |
- | init | 0.000000 | 0 | 0 |
- | optimizing | 0.023255 | 0 | 0 |
- | statistics | 0.000118 | 0 | 0 |
- | preparing | 0.000041 | 0 | 0 |
- | executing | 0.000033 | 0 | 0 |
- | Sending data | 0.003833 | 0 | 0 |
- | end | 0.000054 | 0 | 0 |
- | query end | 0.000045 | 0 | 0 |
- | closing tables | 0.000045 | 0 | 0 |
- | freeing items | 0.000072 | 0 | 0 |
- | Waiting for query cache lock | 0.000033 | 0 | 0 |
- | freeing items | 0.000785 | 0 | 0 |
- | Waiting for query cache lock | 0.000016 | 0 | 0 |
- | freeing items | 0.000002 | 0 | 0 |
- | storing result in query cache | 0.000005 | 0 | 0 |
- | logging slow query | 0.000003 | 0 | 0 |
- | cleaning up | 0.000004 | 0 | 0 |
- +--------------------------------+----------+--------------+---------------+
- 24 rows in set (0.00 sec)
mysql> select min(seq) seq,state,count(*) numb_ops,
-> round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur,
-> round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu
-> from information_schema.profiling
-> where query_id = 5
-> group by state
-> order by seq;
+------+--------------------------------+----------+---------+---------+---------+---------+
| seq | state | numb_ops | sum_dur | avg_dur | sum_cpu | avg_cpu |
+------+--------------------------------+----------+---------+---------+---------+---------+
| 2 | starting | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
| 3 | Waiting for query cache lock | 4 | 0.00006 | 0.00002 | 0.00100 | 0.00025 |
| 4 | checking query cache for query | 1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |
| 5 | checking permissions | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
| 6 | Opening tables | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
| 7 | System lock | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
| 9 | init | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
| 10 | optimizing | 1 | 0.02326 | 0.02326 | 0.00000 | 0.00000 |
| 11 | statistics | 1 | 0.00012 | 0.00012 | 0.00000 | 0.00000 |
| 12 | preparing | 1 | 0.00004 | 0.00004 | 0.00000 | 0.00000 |
| 13 | executing | 1 | 0.00003 | 0.00003 | 0.00000 | 0.00000 |
| 14 | Sending data | 1 | 0.00383 | 0.00383 | 0.00300 | 0.00300 |
| 15 | end | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
| 16 | query end | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
| 17 | closing tables | 1 | 0.00005 | 0.00005 | 0.00000 | 0.00000 |
| 18 | freeing items | 3 | 0.00086 | 0.00029 | 0.00000 | 0.00000 |
| 23 | storing result in query cache | 1 | 0.00001 | 0.00001 | 0.00000 | 0.00000 |
| 24 | logging slow query | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
| 25 | cleaning up | 1 | 0.00000 | 0.00000 | 0.00000 | 0.00000 |
+------+--------------------------------+----------+---------+---------+---------+---------+
19 rows in set (0.00 sec) mysql> show profile cpu for query 5;
+--------------------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+--------------------------------+----------+----------+------------+
| starting | 0.000030 | 0.000000 | 0.000000 |
| Waiting for query cache lock | 0.000005 | 0.000000 | 0.000000 |
| checking query cache for query | 0.000043 | 0.000000 | 0.000000 |
| checking permissions | 0.000007 | 0.000000 | 0.000000 |
| Opening tables | 0.000027 | 0.000000 | 0.000000 |
| System lock | 0.000010 | 0.000000 | 0.000000 |
| Waiting for query cache lock | 0.000010 | 0.001000 | 0.000000 |
| init | 0.000000 | 0.000000 | 0.000000 |
| optimizing | 0.023255 | 0.000000 | 0.000000 |
| statistics | 0.000118 | 0.000000 | 0.000000 |
| preparing | 0.000041 | 0.000000 | 0.000000 |
| executing | 0.000033 | 0.000000 | 0.000000 |
| Sending data | 0.003833 | 0.002999 | 0.001000 |
| end | 0.000054 | 0.000000 | 0.000000 |
| query end | 0.000045 | 0.000000 | 0.000000 |
| closing tables | 0.000045 | 0.000000 | 0.000000 |
| freeing items | 0.000072 | 0.000000 | 0.000000 |
| Waiting for query cache lock | 0.000033 | 0.000000 | 0.000000 |
| freeing items | 0.000785 | 0.000000 | 0.000000 |
| Waiting for query cache lock | 0.000016 | 0.000000 | 0.000000 |
| freeing items | 0.000002 | 0.000000 | 0.000000 |
| storing result in query cache | 0.000005 | 0.000000 | 0.000000 |
| logging slow query | 0.000003 | 0.000000 | 0.000000 |
| cleaning up | 0.000004 | 0.000000 | 0.000000 |
+--------------------------------+----------+----------+------------+
24 rows in set (0.00 sec) mysql> show profile block io for query 5;
+--------------------------------+----------+--------------+---------------+
| Status | Duration | Block_ops_in | Block_ops_out |
+--------------------------------+----------+--------------+---------------+
| starting | 0.000030 | 0 | 0 |
| Waiting for query cache lock | 0.000005 | 0 | 0 |
| checking query cache for query | 0.000043 | 0 | 0 |
| checking permissions | 0.000007 | 0 | 0 |
| Opening tables | 0.000027 | 0 | 0 |
| System lock | 0.000010 | 0 | 0 |
| Waiting for query cache lock | 0.000010 | 0 | 0 |
| init | 0.000000 | 0 | 0 |
| optimizing | 0.023255 | 0 | 0 |
| statistics | 0.000118 | 0 | 0 |
| preparing | 0.000041 | 0 | 0 |
| executing | 0.000033 | 0 | 0 |
| Sending data | 0.003833 | 0 | 0 |
| end | 0.000054 | 0 | 0 |
| query end | 0.000045 | 0 | 0 |
| closing tables | 0.000045 | 0 | 0 |
| freeing items | 0.000072 | 0 | 0 |
| Waiting for query cache lock | 0.000033 | 0 | 0 |
| freeing items | 0.000785 | 0 | 0 |
| Waiting for query cache lock | 0.000016 | 0 | 0 |
| freeing items | 0.000002 | 0 | 0 |
| storing result in query cache | 0.000005 | 0 | 0 |
| logging slow query | 0.000003 | 0 | 0 |
| cleaning up | 0.000004 | 0 | 0 |
+--------------------------------+----------+--------------+---------------+
24 rows in set (0.00 sec)
再看同结构数据的MyISAM表
- mysql> create table payment_myisam like payment;
- Query OK, 0 rows affected (0.03 sec)
- mysql> alter table payment_myisam engine=myisam;
- Query OK, 0 rows affected (0.03 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> show create table payment_myisam\G
- *************************** 1. row ***************************
- Table: payment_myisam
- Create Table: CREATE TABLE `payment_myisam` (
- `payment_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
- `customer_id` smallint(5) unsigned NOT NULL,
- `staff_id` tinyint(3) unsigned NOT NULL,
- `rental_id` int(11) DEFAULT NULL,
- `amount` decimal(5,2) NOT NULL,
- `payment_date` datetime NOT NULL,
- `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- PRIMARY KEY (`payment_id`),
- KEY `idx_fk_staff_id` (`staff_id`),
- KEY `idx_fk_customer_id` (`customer_id`),
- KEY `fk_payment_rental` (`rental_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
- mysql> insert into payment_myisam select * from payment;
- Query OK, 16049 rows affected (0.10 sec)
- Records: 16049 Duplicates: 0 Warnings: 0
- mysql> create table payment_myisam like payment;
- Query OK, 0 rows affected (0.03 sec)
- mysql> alter table payment_myisam engine=myisam;
- Query OK, 0 rows affected (0.03 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> show create table payment_myisam\G
- *************************** 1. row ***************************
- Table: payment_myisam
- Create Table: CREATE TABLE `payment_myisam` (
- `payment_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
- `customer_id` smallint(5) unsigned NOT NULL,
- `staff_id` tinyint(3) unsigned NOT NULL,
- `rental_id` int(11) DEFAULT NULL,
- `amount` decimal(5,2) NOT NULL,
- `payment_date` datetime NOT NULL,
- `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- PRIMARY KEY (`payment_id`),
- KEY `idx_fk_staff_id` (`staff_id`),
- KEY `idx_fk_customer_id` (`customer_id`),
- KEY `fk_payment_rental` (`rental_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
- mysql> insert into payment_myisam select * from payment;
- Query OK, 16049 rows affected (0.10 sec)
- Records: 16049 Duplicates: 0 Warnings: 0
mysql> create table payment_myisam like payment;
Query OK, 0 rows affected (0.03 sec) mysql> alter table payment_myisam engine=myisam;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table payment_myisam\G
*************************** 1. row ***************************
Table: payment_myisam
Create Table: CREATE TABLEpayment_myisam
(
payment_id
smallint(5) unsigned NOT NULL AUTO_INCREMENT,
customer_id
smallint(5) unsigned NOT NULL,
staff_id
tinyint(3) unsigned NOT NULL,
rental_id
int(11) DEFAULT NULL,
amount
decimal(5,2) NOT NULL,
payment_date
datetime NOT NULL,
last_update
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (payment_id
),
KEYidx_fk_staff_id
(staff_id
),
KEYidx_fk_customer_id
(customer_id
),
KEYfk_payment_rental
(rental_id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec) mysql> insert into payment_myisam select * from payment;
Query OK, 16049 rows affected (0.10 sec)
Records: 16049 Duplicates: 0 Warnings: 0
- mysql> select count(*) from payment_myisam;
- +----------+
- | count(*) |
- +----------+
- | 16049 |
- +----------+
- 1 row in set (0.01 sec)
- mysql> show profiles;
- | 23 | 0.09449600 | insert into payment_myisam select * from payment |
- | 24 | 0.00023500 | select count(*) from payment_myisam |
- 15 rows in set (0.00 sec)
- mysql> select count(*) from payment_myisam;
- +----------+
- | count(*) |
- +----------+
- | 16049 |
- +----------+
- 1 row in set (0.01 sec)
- mysql> show profiles;
- | 23 | 0.09449600 | insert into payment_myisam select * from payment |
- | 24 | 0.00023500 | select count(*) from payment_myisam |
- 15 rows in set (0.00 sec)
mysql> select count(*) from payment_myisam;
+----------+
| count(*) |
+----------+
| 16049 |
+----------+
1 row in set (0.01 sec) mysql> show profiles; | 23 | 0.09449600 | insert into payment_myisam select * from payment |
| 24 | 0.00023500 | select count(*) from payment_myisam | 15 rows in set (0.00 sec)
- mysql> show profile for query 24;
- +--------------------------------+----------+
- | Status | Duration |
- +--------------------------------+----------+
- | starting | 0.000026 |
- | Waiting for query cache lock | 0.000003 |
- | checking query cache for query | 0.000037 |
- | checking permissions | 0.000006 |
- | Opening tables | 0.000015 |
- | System lock | 0.000009 |
- | Waiting for query cache lock | 0.000019 |
- | init | 0.000011 |
- | optimizing | 0.000006 |
- | executing | 0.000006 |
- | end | 0.000000 |
- | query end | 0.000001 |
- | closing tables | 0.000008 |
- | freeing items | 0.000019 |
- | Waiting for query cache lock | 0.000008 |
- | freeing items | 0.000013 |
- | Waiting for query cache lock | 0.000008 |
- | freeing items | 0.000008 |
- | storing result in query cache | 0.000015 |
- | logging slow query | 0.000008 |
- | cleaning up | 0.000009 |
- +--------------------------------+----------+
查看mysql正在执行的SQL语句,使用profile分析SQL执行状态的更多相关文章
- 查看SQL语句执行时间与测试SQL语句性能
查看SQL语句执行时间与测试SQL语句性能 写程序的人,往往需要分析所写的SQL语句是否够优化.是否能提升执行效率,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了 ...
- 戈多编程-小谈sql语句的优化分析
在sqlserver大数据查询中,避免不了查询效率减慢,暂且抛弃硬件原因和版本原因,仅从sql语句角度分析. 一. sql 语句性能不达标,主要原因有一下几点: 1. 未建索引,检索导致全表扫描 2. ...
- 解决死锁之路3 - 常见 SQL 语句的加锁分析 (转)
出处:https://www.aneasystone.com/archives/2017/12/solving-dead-locks-three.html 这篇博客将对一些常见的 SQL 语句进行加锁 ...
- DLA SQL分析函数:SQL语句审计与分析的利器
1. 简介 Data Lake Analytics(https://www.aliyun.com/product/datalakeanalytics)最新release一组SQL内置函数,用来进行SQ ...
- Oracle sql共享池$sqlarea分析SQL资源使用情况
遇到需要排查一个系统使用sql的情况,可以通过查询Oracle的$sql.$ssssion.$sqlarea进行统计排查 排查时可以先看一下$sql和$session的基本信息 select * fr ...
- 查看Mysql实时执行的Sql语句
最近给客户开发了基于Asp.Net mvc5 +Mysql+EF的项目,但是在EF里无法看到Mysql执行的语句 之前也找到一些监控Mysql的软件但一直没有用起来,现在又遇到了问题即在EF里Mysa ...
- 使用Hibernate 拦截执行sql语句,并输出sql语句,获取sql语句
重建包名 org.hibernate.type.descriptor.sql 重建类BasicBinder 代码如下 package org.hibernate.type.descriptor.sql ...
- 查看MySQL正在执行的线程
一.使用SQL语句查询正在执行的线程 SHOW PROCESSLIST; 二.使用kill 线程id就可以结束线程(引起数据变化的线程需特别小心) SHOW PROCESSLIST; +------+ ...
- 【PL/SQL练习】游标cursor :oracle 在执行sql语句时,为sql语句所分配的一个私有的内存区域
隐式游标:一次只能返回一行结果(不需要定义,默认自动建立) 显式游标: 需要开发人员提前定义,可以通过循环的方式处理游标里的sql语句,返回多行结果 隐式游标的属性: sql%rowcou ...
随机推荐
- Generating SSH Keys for github
由于最近电脑重装了Windows 8.1, 想用github维护一些代码.故不得不重新生成一下ssh key. 按https://help.github.com/articles/generating ...
- 优动漫PAINT-超简单灌木教程
超简单灌木教程~零基础神马的都能神还原哦! 优动漫PAINT下载:http://wm.makeding.com/iclk/?zoneid=18597 想要Get到更多有关优动漫的信息包括软件下载,可关 ...
- 模板层 Template
每一个 Web 框架都需要一种很便利的方法用于动态生成 HTML 页面. 最常见的做法是使用模板. 模板包含所需 HTML 页面的静态部分,以及一些特殊的模版语法,用于将动态内容插入静态部分. 说白了 ...
- 字符串格式化输出、while循环、运算符、编码
1.字符串格式化输出 %占位符: %s => 字符串 %d=>整数型 %%=>转义 普通的% %()不能多,不能少,一一对应 f"{}"大括号里的内容一般都放变量 ...
- 移动端的vue项目,启动错误:Module build failed: Error: No PostCSS Config found in:
新建一个postcss.config.js 写上下面代码 `module.exports = { plugins: { 'autoprefixer': {browsers: 'last 5 versi ...
- dedecms简略标题(副标题)使用方法教程
在常见的CMS系统中,我对dedecms算是比较熟悉的,自己网站用的也是这个系统.系统功能强大使用灵活,相信这也是它受到大多数中小站长青睐的原因. 再好的系统也有照顾不周的地方,很多站长也会有自己个性 ...
- [NOI2014]动物园(KMP)
题意 题解 因为,一直用j=nxt[j]来遍历,可以遍历前i个字符所有相等的前后缀长度,所以有一个暴力的想法,就是对于每一个长度,开始遍历,记录长度小于i/2的相等的前后缀数量,最后累加即可. 但显然 ...
- mysql 定时每秒插入一条数据
1.创建表 2.创建存储过程 CREATE PROCEDURE user()INSERT INTO user(name,sex) VALUES ('1111','1'); 3.创建定时器 CREATE ...
- C语言过程活动记录
C 语言自动提供的服务之一就是跟踪调用链——哪些函数调用了哪些函数,当下一个return语句执行后,控制将返回何处等.解决这个问题的经典机制是堆栈中的活动记录. 当每个函数被调用时,都会产生一个过程记 ...
- rest_framework-节流-总结完结篇
列表从后往前读 #1.在request中获取IP#2.访问记录 VISIT_RECORD = {} 放缓存 数据库 都可以 建议缓存import timeclass VisitThrottle(obj ...