在通过查看执行计划explain外,优化一条sql语句外的另一个诊断工具profiler,在5.0以后加入了mysql大家庭。其使用在后续的版本中得到逐步的丰富和扩展。以下的相关测试是在5.5版本中的测试,有不同之处请查看官网的相关文档说明为准。
要优化一条sql,需要遭到这条sql的性能瓶颈,找到消耗资源的地方在哪里,比如说,CPU,IO,OPS等等,在mysql5.0.37版本以后当中,可以使用profiling功能进行这些数据信息的定位。
其作为一种非常方便的性能诊断工具,可以获取各种资源的消耗数据情况。show profile 的格式如下:
SHOW PROFILE [type [, type] ... ]
[FOR QUERY n]
[LIMIT row_count [OFFSET offset]]
type:
ALL | BLOCK IO | CONTEXT SWITCHES | CPU | IPC | MEMORY | PAGE FAULTS | SOURCE | SWAPS
首先要开启profiling参数
set profiling=1 开启
set profiling=0 关闭(默认)
开启功能后,mysql机会记录所有的sql查询的profiler的信息;
其参数默认是0,关闭状态;
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT a.id,a.west_name,a.west_class_id,a.west_way, a.west_med_name,a.release_time,a.is_check,u.user_name,m.med_name
-> FROM msp.msp_western_medicine a
-> JOIN msp.msp_user u ON u.id=a.user_id
-> JOIN msp.msp_medcine_company m ON m.id=a.med_id
-> WHERE 1=1 AND a.is_check <> 2 AND a.west_class_id = '2' ORDER BY a.id DESC LIMIT 10;
+------+-----------+---------------+----------+---------------+---------------------+----------+---------------+----------------+
| id | west_name | west_class_id | west_way | west_me