MySQL中处理慢sql时,如果看完执行计划之后还拿不定优化的方案的时候,可能需要借助profile的来看一看具体慢在哪里,整个过程如下:
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2016-07-11 11:10:37 |
+---------------------+
1 row in set (0.00 sec)
mysql> show profiles;
+----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | 4.79560350 | SELECT rgu.user_id as recipient_id, 0 as recipient_type, srg.alarm_settings_id, srg.recipient_group_id, rg.notification_type, u.agreement_id, u.name, u.email, u.mobile_no, u.wechat_no, u.notification_hour_from, u.notification_hour_to
FROM NL_U_ALARM_SETTINGS_RECIPIENT_GROUP srg
INNER JOIN NL_U_ALAR |
+----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)
mysql> set @query_id=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select state,sum(duration) as Total_R, round(100*sum(duration)/(select sum(duration) from information_schema.profiling where query_id=@query_id),2) as Pct_R, count(*) as Calls, sum(duration)/count(*) as "R/Call" from information_schema.profiling where query_id=@query_id group by state order by Total_R desc;
+----------------------+----------+-------+-------+--------------+
| state | Total_R | Pct_R | Calls | R/Call |
+----------------------+----------+-------+-------+--------------+
| Sending data | 4.753893 | 99.13 | 4 | 1.1884732500 |
| removing tmp table | 0.038097 | 0.79 | 1 | 0.0380970000 |
| System lock | 0.000906 | 0.02 | 1 | 0.0009060000 |
| Opening tables | 0.000517 | 0.01 | 1 | 0.0005170000 |
| checking permissions | 0.000354 | 0.01 | 8 | 0.0000442500 |
| preparing | 0.000332 | 0.01 | 3 | 0.0001106667 |
| optimizing | 0.000276 | 0.01 | 3 | 0.0000920000 |
| starting | 0.000262 | 0.01 | 1 | 0.0002620000 |
| executing | 0.000220 | 0.00 | 3 | 0.0000733333 |
| statistics | 0.000212 | 0.00 | 3 | 0.0000706667 |
| closing tables | 0.000185 | 0.00 | 1 | 0.0001850000 |
| freeing items | 0.000164 | 0.00 | 1 | 0.0001640000 |
| cleaning up | 0.000128 | 0.00 | 1 | 0.0001280000 |
| logging slow query | 0.000045 | 0.00 | 1 | 0.0000450000 |
| query end | 0.000013 | 0.00 | 1 | 0.0000130000 |
+----------------------+----------+-------+-------+--------------+
15 rows in set (0.01 sec)
mysql> set profiling=0;
Query OK, 0 rows affected (0.00 sec)
转载于:https://blog.51cto.com/quenlang/1825268