Mysql profile 的应用

本文介绍如何使用MySQL的profile功能来分析SQL语句的执行开销。通过设置profiling参数并执行特定SQL,可以获取详细的执行时间分布,从而定位性能瓶颈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何通过profile来发现语句的开销

构造场景:
开启一个回话,执行语句
lock table film_text read;

开启另外一个回话,执行下面语句:
root@sakila 07:51:14>show variables like '%profil%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+
3 rows in set (0.01 sec)

root@sakila 07:52:38>set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

root@sakila 07:52:45>update film_text set title='test' where film_id=1003;
Query OK, 0 rows affected (11.02 sec)
Rows matched: 1  Changed: 0  Warnings: 0


root@sakila 07:53:18>show profiles;
+----------+-------------+------------------------------------------------------+
| Query_ID | Duration    | Query                                                |
+----------+-------------+------------------------------------------------------+
|        1 | 11.02395150 | update film_text set title='test' where film_id=1003 |
+----------+-------------+------------------------------------------------------+
1 row in set, 1 warning (0.01 sec)


root@sakila 07:53:26>show profile for query 1;
+------------------------------+-----------+
| Status                       | Duration  |
+------------------------------+-----------+
| starting                     |  0.000793 |
| checking permissions         |  0.000032 |
| Opening tables               |  0.000087 |
| init                         |  0.000026 |
| System lock                  |  0.000025 |
| Waiting for table level lock | 11.018648 |
| System lock                  |  0.000770 |
| updating                     |  0.002947 |
| end                          |  0.000077 |
| query end                    |  0.000069 |
| closing tables               |  0.000062 |
| freeing items                |  0.000253 |
| cleaning up                  |  0.000163 |
+------------------------------+-----------+
13 rows in set, 1 warning (0.00 sec)

可以看到开销是在这个waiting table lock.

貌似 show profiles 只能显示本回话执行的sql语句的情况,即使设置了global profiling=1

所以如果要调优一组sql, 那么把这一组sql放到一个回话中执行,然后执行show profiles 查看每条语句的执行时间,并且进一步分析开销在什么地方。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/674865/viewspace-2135270/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/674865/viewspace-2135270/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值