MySQL -- 调优查看

执行计划(explain):
mysql> explain select * from (select * from learn_notbook a) b;
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |   55 |       |
|  2 | DERIVED     | a          | ALL  | NULL          | NULL | NULL    | NULL |   51 |       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
2 rows in set (0.06 sec)


mysql> explain extended  select * from (select * from learn_notbook a) b;
+----+-------------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |   55 |   100.00 |       |
|  2 | DERIVED     | a          | ALL  | NULL          | NULL | NULL    | NULL |   51 |   100.00 |       |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+-------+
2 rows in set, 1 warning (0.03 sec)


mysql> explain partitions  select * from (select * from learn_notbook a) b;
+----+-------------+------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table      | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+-------+
|  1 | PRIMARY     | <derived2> | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   55 |       |
|  2 | DERIVED     | a          | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   51 |       |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+-------+
2 rows in set (0.03 sec)


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


mysql> select a.type,count(*) from learn_notbook a group by a.type;
+------+----------+
| type | count(*) |
+------+----------+
| 1    |       35 |
| 2    |       12 |
| 3    |        8 |
+------+----------+
3 rows in set (0.00 sec)


mysql> show profiles\g
+----------+------------+-------------------------------------------------------------+
| Query_ID | Duration   | Query                                                       |
+----------+------------+-------------------------------------------------------------+
|        1 | 0.00167250 | select a.type,count(*) from learn_notbook a group by a.type |
+----------+------------+-------------------------------------------------------------+
1 row in set (0.04 sec)
mysql> show profile CPU,BLOCK IO FOR QUERY 1;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000131 | 0.000000 |   0.000000 |         NULL |          NULL |
| checking permissions | 0.000017 | 0.000000 |   0.000000 |         NULL |          NULL |
| Opening tables       | 0.000417 | 0.000000 |   0.000000 |         NULL |          NULL |
| System lock          | 0.000027 | 0.000000 |   0.000000 |         NULL |          NULL |
| init                 | 0.000049 | 0.000000 |   0.000000 |         NULL |          NULL |
| optimizing           | 0.000009 | 0.000000 |   0.000000 |         NULL |          NULL |
| statistics           | 0.000027 | 0.000000 |   0.000000 |         NULL |          NULL |
| preparing            | 0.000022 | 0.000000 |   0.000000 |         NULL |          NULL |
| Creating tmp table   | 0.000282 | 0.000000 |   0.000000 |         NULL |          NULL |
| executing            | 0.000008 | 0.000000 |   0.000000 |         NULL |          NULL |
| Copying to tmp table | 0.000435 | 0.000000 |   0.000000 |         NULL |          NULL |
| Sorting result       | 0.000049 | 0.000000 |   0.000000 |         NULL |          NULL |
| Sending data         | 0.000035 | 0.000000 |   0.000000 |         NULL |          NULL |
| end                  | 0.000007 | 0.000000 |   0.000000 |         NULL |          NULL |
| removing tmp table   | 0.000019 | 0.000000 |   0.000000 |         NULL |          NULL |
| end                  | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| query end            | 0.000009 | 0.000000 |   0.000000 |         NULL |          NULL |
| closing tables       | 0.000013 | 0.000000 |   0.000000 |         NULL |          NULL |
| freeing items        | 0.000105 | 0.000000 |   0.000000 |         NULL |          NULL |
| logging slow query   | 0.000005 | 0.000000 |   0.000000 |         NULL |          NULL |
| cleaning up          | 0.000004 | 0.000000 |   0.000000 |         NULL |          NULL |
+----------------------+----------+----------+------------+--------------+---------------+
21 rows in set (0.03 sec)


mysql> show profile cpu,block io,memory,swaps,context switches,source for query 1;
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+-----------------+---------------+-------------+
| Status               | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Swaps | Source_function | Source_file   | Source_line |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+-----------------+---------------+-------------+
| starting             | 0.000104 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | NULL            | NULL          |        NULL |
| checking permissions | 0.000013 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_parse.cc  |        4745 |
| Opening tables       | 0.000135 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_base.cc   |        4837 |
| System lock          | 0.000040 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | lock.cc       |         299 |
| init                 | 0.000047 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        2554 |
| optimizing           | 0.000009 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |         863 |
| statistics           | 0.000034 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        1054 |
| preparing            | 0.000020 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        1076 |
| Creating tmp table   | 0.000323 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        1592 |
| executing            | 0.000006 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        1823 |
| Copying to tmp table | 0.000417 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        1970 |
| Sorting result       | 0.000043 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        2253 |
| Sending data         | 0.000028 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        2365 |
| end                  | 0.000006 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |        2590 |
| removing tmp table   | 0.000017 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |       11179 |
| end                  | 0.000005 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_select.cc |       11204 |
| query end            | 0.000008 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_parse.cc  |        4434 |
| closing tables       | 0.000013 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_parse.cc  |        4486 |
| freeing items        | 0.000217 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_parse.cc  |        5634 |
| logging slow query   | 0.000006 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_parse.cc  |        1460 |
| cleaning up          | 0.000004 | 0.000000 |   0.000000 |              NULL |                NULL |         NULL |          NULL |  NULL | <unknown>       | sql_parse.cc  |        1416 |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+-------+-----------------+---------------+-------------+


mysql> show variables like 'profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | ON    |
| profiling_history_size | 15    |
+------------------------+-------+

2 rows in set (0.00 sec)



mysql的sql语句优化都使用explain,但是这个没有办法知道详细的Memory/CPU等使用量
MySQL Query Profiler, 可以查询到此 SQL 语句会执行多少, 并看出 CPU/Memory 使用


量, 执行过程 System lock, Table lock 花多少时间等等. 
mysql> show variables like 'profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | OFF   | 
| profiling_history_size | 15    | 
+------------------------+-------+
开启此功能
mysql>set profiling=1; 
mysql> show variables like 'profiling%';
# 此命令会让mysql在 information_schema 的 database 建立一個 PROFILING 的 


table 来记录.
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | ON    | 
| profiling_history_size | 15    | 
+------------------------+-------+
profiling_history_size记录多少次查询


mysql> show profiles;
+----------+------------+------------------------------------+
| Query_ID | Duration   | Query                              |
+----------+------------+------------------------------------+
|        1 | 0.00018100 | show variables like 'profiling%'   | 
|        2 | 0.00020400 | show variables like 'profiling%'   | 
|        3 | 0.00007800 | set profiling=1                    | 
|        4 | 0.00011000 | show variables like 'profiling%'   | 
|        5 | 0.00002400 | select count(1) from `mrhao_stats` | 
|        6 | 1.52181400 | select count(*) from `mrhao_stats` | 
|        7 | 0.00026900 | show variables like 'profiling%'   | 


mysql> show profile for query 6;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| (initialization)               | 0.000003 | 
| checking query cache for query | 0.000042 | 
| Opening tables                 | 0.00001 | 
| System lock                    | 0.000004 | 
| Table lock                     | 0.000025 | 
| init                           | 0.000009 | 
| optimizing                     | 0.000003 | 
| statistics                     | 0.000007 | 
| preparing                      | 0.000007 | 
| executing                      | 0.000004 | 
| Sending data                   | 1.521676 | 
| end                            | 0.000007 | 
| query end                      | 0.000003 | 
| storing result in query cache | 0.000002 | 
| freeing items                  | 0.000006 | 
| closing tables                 | 0.000004 | 
| logging slow query             | 0.000002 | 
+--------------------------------+----------+
17 rows in set (0.00 sec)


mysql> show profile cpu for query 6;
+--------------------------------+----------+----------+------------+
| Status                         | Duration | CPU_user | CPU_system |
+--------------------------------+----------+----------+------------+
| (initialization)               | 0.000003 | 0        | 0          | 
| checking query cache for query | 0.000042 | 0.001    | 0          | 
| Opening tables                 | 0.00001 | 0        | 0          | 
| System lock                    | 0.000004 | 0        | 0          | 
| Table lock                     | 0.000025 | 0        | 0          | 
| init                           | 0.000009 | 0        | 0          | 
| optimizing                     | 0.000003 | 0        | 0          | 
| statistics                     | 0.000007 | 0        | 0          | 
| preparing                      | 0.000007 | 0        | 0          | 
| executing                      | 0.000004 | 0        | 0          | 
| Sending data                   | 1.521676 | 1.631752 | 0.036995   | 
| end                            | 0.000007 | 0        | 0          | 
| query end                      | 0.000003 | 0        | 0          | 
| storing result in query cache | 0.000002 | 0        | 0          | 
| freeing items                  | 0.000006 | 0        | 0          | 
| closing tables                 | 0.000004 | 0        | 0          | 
| logging slow query             | 0.000002 | 0        | 0          | 
+--------------------------------+----------+----------+------------+
17 rows in set (0.00 sec)
    * ALL - displays all information
    * BLOCK IO - displays counts for block input and output operations
    * CONTEXT SWITCHES - displays counts for voluntary and involuntary 


context switches
    * IPC - displays counts for messages sent and received
    * MEMORY - is not currently implemented
    * PAGE FAULTS - displays counts for major and minor page faults
    * 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
    * SWAPS - displays swap counts


查询时间跟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 = 7
    group by state
    order by seq;
关闭此功能


mysql> set profiling=0;
mysql> show variables like 'profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | OFF   | 
| profiling_history_size | 15    | 
+------------------------+-------+


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值