mysql 方法或者存储过程执行慢的调试方法

来源:互联网 发布:网络用语qq爱是什么 编辑:程序博客网 时间:2024/05/02 02:25
第一步:修改/etc/my.cnf文件,找到[mysqld] 里面加入

#执行的sqllog=/tmp/logs/mysqld.log #记录sql执行超过下面设置时间的sqllog-slow-queries = /tmp/mysqlslowquery.log#执行时间大于等于1秒long_query_time = 1 

然后你可以tail -f /tmp/logs/mysqld.log 监控所有执行的sql,同样的方法可以监控mysqlslowquery.log 为执行时间超过long_query_time = 1(秒)的sql语句

比如通过第一步我们找到了某一个mysql 自定义函数执行慢func_getDevice(); 执行了15s,但并不知道这个方法里面到底是那一条sql影响了性能,那么就有了第二步。

第二步:进入mysql命令行,输入

mysql> set profiling=1;mysql> select func_getDevice(1);mysql> show profiles;+----------+------------+-----------------------+| Query_ID | Duration   | Query                 |+----------+------------+-----------------------+|        1 | 0.00250400 | select * from TDevice |+----------+------------+-----------------------+1 row in set (0.00 sec)


这时候你就会看到一个详细的sql执行列表,但默认只记录15条sql,如果方法里面的sql比较多,那么可以通过设置

mysql> set profiling_history_size=20;mysql> show variables like 'profiling%';+------------------------+-------+| Variable_name          | Value |+------------------------+-------+| profiling              | ON    || profiling_history_size | 15    |+------------------------+-------+2 rows in set (0.00 sec)mysql> select func_getDevice(1);mysql> show profiles;

这是时候就可以准确的看到是那一条sql语句影响了性能,比如 Query_ID=1  select * from TDevice 影响了性能;

mysql> show profile for query 1;详细查看执行一条sql的耗时情况+--------------------------------+----------+| 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 | 

也可以通过

mysql> EXPLAIN select * from TDevice;+----+-------------+---------+------+---------------+------+---------+------+------+-------+| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra |+----+-------------+---------+------+---------------+------+---------+------+------+-------+|  1 | SIMPLE      | TDevice | ALL  | NULL          | NULL | NULL    | NULL |   70 |       |+----+-------------+---------+------+---------------+------+---------+------+------+-------+1 row in set (0.00 sec)

查看表的索引等是否合理,通过针对性的优化以提高效率。


0 0
原创粉丝点击