mysql学习笔记之profiling分析
要想有画一条query,就需要清楚这条query的性能瓶颈到底在哪里,mysql的profiler是一个使用非常方便的查询诊断分析工具,通过该工具可以获取一条查询在整个执行过程中多种资源的消耗情况,例如内存消耗、I/O消耗和CPU消耗等。
profile的语法规则如下:
show profile [type [, type] ...][for query n]
[limit row_count [offset offset]]
其中type参数的可选项含义如下:
- all:显示所有的信息
- block io:限时输入输出操作阻塞的数量
- context switches:显示自动或非自动context switches的数量
- cpu:显示系统和用户cpu使用的时间
- ipc:显示信息发送和接受的数量
- memory:内存的信息
- page faults:显示主要的page faults数量
- source:显示函数的名称,并且系那是函数所在文件的名字和行数
- swaps:显示swap数量
开启profile
MariaDB [community]> set profiling = 1;Query OK, 0 rows affected (0.00 sec)
在开启了query profiler功能之后,mysql就会自动记录所有执行的query的profile信息。下面执行query
select count(*) from admin_user_copyselect count(*) from purchase_order
通过执行 show profile
命令获取当前系统中保存的多个query的profile的概要信息。
MariaDB [community]> show profiles;+----------+------------+--------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------------------------+
| 1 | 1.43076000 | select count(*) from admin_user_copy |
| 2 | 0.35574100 | select count(*) from purchase_order |
+----------+------------+--------------------------------------+
2 rows in set (0.00 sec)
针对单个query获取详细的profile的信息
在获取概要信息之后就可以根据概要信息中的query_id来获取某一个query在执行过程中的profile信息了
MariaDB [community]> show profile for query 1;+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000069 |
| checking permissions | 0.000019 |
| Opening tables | 0.000035 |
| After opening tables | 0.000007 |
| System lock | 0.000005 |
| Table lock | 0.000014 |
| init | 0.000015 |
| optimizing | 0.000010 |
| statistics | 0.000019 |
| preparing | 0.000021 |
| executing | 0.000006 |
| Sending data | 1.430456 |
| end | 0.000020 |
| query end | 0.000010 |
| closing tables | 0.000003 |
| Unlocking tables | 0.000013 |
| freeing items | 0.000006 |
| updating status | 0.000027 |
| cleaning up | 0.000005 |
+----------------------+----------+
MariaDB [community]> show profile cpu,block io for query 1;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000069 | 0.000058 | 0.000011 | 0 | 0 |
| checking permissions | 0.000019 | 0.000007 | 0.000011 | 0 | 0 |
| Opening tables | 0.000035 | 0.000033 | 0.000002 | 0 | 0 |
| After opening tables | 0.000007 | 0.000005 | 0.000002 | 0 | 0 |
| System lock | 0.000005 | 0.000004 | 0.000001 | 0 | 0 |
| Table lock | 0.000014 | 0.000008 | 0.000006 | 0 | 0 |
| init | 0.000015 | 0.000014 | 0.000002 | 0 | 0 |
| optimizing | 0.000010 | 0.000007 | 0.000001 | 0 | 0 |
| statistics | 0.000019 | 0.000018 | 0.000001 | 0 | 0 |
| preparing | 0.000021 | 0.000019 | 0.000002 | 0 | 0 |
| executing | 0.000006 | 0.000006 | 0.000002 | 0 | 0 |
| Sending data | 1.430456 | 1.928391 | 0.113126 | 0 | 0 |
| end | 0.000020 | 0.000008 | 0.000013 | 0 | 0 |
| query end | 0.000010 | 0.000008 | 0.000001 | 0 | 0 |
| closing tables | 0.000003 | 0.000002 | 0.000002 | 0 | 0 |
| Unlocking tables | 0.000013 | 0.000012 | 0.000001 | 0 | 0 |
| freeing items | 0.000006 | 0.000005 | 0.000001 | 0 | 0 |
| updating status | 0.000027 | 0.000007 | 0.000020 | 0 | 0 |
| cleaning up | 0.000005 | 0.000003 | 0.000001 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+
来自六星教育学习笔记
以上是 mysql学习笔记之profiling分析 的全部内容, 来源链接: utcz.com/z/534120.html