mysql 快速定位cpu 占比过高的sql语句
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当MySQL数据库的CPU<span style="color: black;">运用</span>率<span style="color: black;">反常</span><span style="color: black;">上升</span>时,定位<span style="color: black;">引起</span>问题的SQL语句<span style="color: black;">能够</span><span style="color: black;">经过</span>以下<span style="color: black;">过程</span>进行:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. **<span style="color: black;">运用</span>`top`或`pidstat`命令**:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>,<span style="color: black;">能够</span><span style="color: black;">经过</span>`top -H -p <mysqld进程id>`<span style="color: black;">或</span>`pidstat -t -p <mysqld进程id> 1 5`命令来确定哪个线程占用了大量的CPU资源。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. **<span style="color: black;">查找</span>`performance_schema`或`information_schema`**:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">运用</span>以下SQL语句<span style="color: black;">能够</span><span style="color: black;">查找</span>到<span style="color: black;">详细</span>的线程信息,<span style="color: black;">包含</span>其操作系统线程ID(`THREAD_OS_ID`)和正在执行的SQL语句:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```sql</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.THREAD_OS_ID,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.id,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.USER,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.HOST,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.db,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.PROCESSLIST_INFO,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.INFO</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PERFORMANCE_SCHEMA.THREADS a,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">information_schema.PROCESSLIST b</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.id = a.processlist_id AND a.THREAD_OS_ID = <<span style="color: black;">详细</span>线程id>;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 其中 `<<span style="color: black;">详细</span>线程id>` 是从<span style="color: black;">过程</span>1中获取的线程ID。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. **分析慢<span style="color: black;">查找</span>日志**:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 开启MySQL的慢<span style="color: black;">查找</span>日志,分析执行时间过长的SQL语句。<span style="color: black;">能够</span><span style="color: black;">经过</span>以下命令查看慢<span style="color: black;">查找</span>日志的配置:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```sql</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SHOW VARIABLES LIKE slow_query_log%;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">而后</span><span style="color: black;">运用</span>`explain`分析慢<span style="color: black;">查找</span>日志中记录的SQL语句,找出可能的性能瓶颈。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. **<span style="color: black;">运用</span>`SHOW PROCESSLIST`命令**:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">登录到MySQL服务器,<span style="color: black;">运用</span>`SHOW FULL PROCESSLIST;`命令查看当前正在执行的所有线程,<span style="color: black;">包含</span>它们的状态和执行时间。这有助于识别<span style="color: black;">长期</span>运行的<span style="color: black;">查找</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5. **<span style="color: black;">运用</span>`SHOW PROFILE`或`SHOW PROFILES`**:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">针对</span><span style="color: black;">短期</span>执行但CPU<span style="color: black;">运用</span>率高的SQL,<span style="color: black;">能够</span><span style="color: black;">运用</span>`SHOW PROFILE`或`SHOW PROFILES`来获取SQL语句的<span style="color: black;">仔细</span>性能数据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6. **<span style="color: black;">检测</span>`information_schema`中的`processlist`表**:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">经过</span>以下命令<span style="color: black;">查找</span>`processlist`表,找到CPU<span style="color: black;">运用</span>率高的线程:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```sql</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> SELECT * FROM performance_schema.threads WHERE thread_os_id = 1;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这将<span style="color: black;">表示</span>与特定操作系统线程ID<span style="color: black;">关联</span>联的MySQL线程的<span style="color: black;">仔细</span>信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">7. **<span style="color: black;">运用</span>通用日志记录**:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">倘若</span><span style="color: black;">必须</span>,<span style="color: black;">能够</span>开启MySQL的通用日志记录功能,记录所有执行的SQL语句,以便进行分析:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```sql</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> SET GLOBAL log_output=TABLE;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> SET GLOBAL general_log=ON;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ```</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span><span style="color: black;">经过</span><span style="color: black;">查找</span>`mysql.general_log`表来查看日志记录的SQL语句。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>这些<span style="color: black;">过程</span>,你<span style="color: black;">能够</span>定位到<span style="color: black;">引起</span>CPU<span style="color: black;">运用</span>率高的SQL语句,并进一步分析和优化它们以降低资源消耗。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">下面<span style="color: black;">运用</span><span style="color: black;">方法</span>二来演示定位SQL</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1:top查看</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q8.itc.cn/images01/20240718/e0fead01f5264c6bb5834a46e916a85c.jpeg" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2:top -Hp 42977</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q2.itc.cn/images01/20240718/08bde56aca384721b0394e3c56d1f632.jpeg" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3: 带入下面的脚本</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">performance_schema.events_statements_current</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">thread_id = (</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">thread_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">performance_schema.threads</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">thread_os_id = 10593</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) ;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q3.itc.cn/images01/20240718/a7afee40719b415f883348e0ca8367ce.jpeg" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SELECT</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a. USER,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a. HOST,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.db,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.thread_os_id,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b.thread_id,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.id processlist_id,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.command,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.time,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.state,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.info</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">FROM</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">information_schema.PROCESSLIST a,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">performance_schema.threads b</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WHERE</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a.id = b.processlist_id</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">AND b.thread_os_id = 10593;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q9.itc.cn/images01/20240718/1cae03698c204221a3f1ca46f1767d05.jpeg" style="width: 50%; margin-bottom: 20px;"><a style="color: black;"><span style="color: black;">返回<span style="color: black;">外链论坛:www.fok120.com</span>,查看<span style="color: black;">更加多</span></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">责任编辑:网友投稿</span></p>
我深受你的启发,你的话语是我前进的动力。 回顾历史,我们感慨万千;放眼未来,我们信心百倍。
页:
[1]