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

第一步:修改/etc/my.cnf文件,找到[mysqld] 里面加入

#执行的sql
log=/tmp/logs/mysqld.log 
#记录sql执行超过下面设置时间的sql
log-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)

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值