首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何快速定位当前数据库消耗 CPU 最高的 SQL 语句?

如何快速定位当前数据库消耗 CPU 最高的 SQL 语句?

作者头像
用户1516716
发布2021-07-06 16:50:59
7880
发布2021-07-06 16:50:59
举报

来源:https://www.toutiao.com/i6923526305795293707?wid=1623686217615

概述

如果是Oracle数据库我们可以很容易通过sql来定位到当前数据库中哪些消耗CPU高的语句,而mysql数据库可以怎么定位呢?这里用一个简单例子说明下...

主要是了解如何定位的思路,具体看官网介绍..

参考:https://www.percona.com/blog/2020/04/23/a-simple-approach-to-troubleshooting-high-cpu-in-mysql/

One of our customers recently asked whether it is possible to identify, from the MySQL side, the query that is causing high CPU usage on his system. The usage of simple OS tools to find the culprit has been a widely used technique for a long time by PostgreSQL and Oracle DBAs, but it didn’t work for MySQL as historically we’ve lacked the instrumentation to match an OS thread with an internal processlist thread – until recently. Percona added support to map processlist ids to OS thread ids through column TID of the information_schema.processlist table starting on Percona Server for MySQL 5.6.27. With the release of 5.7, MySQL followed with its own implementation by extending the PERFORMANCE_SCHEMA.THREADS table and adding a new column named THREAD_OS_ID, which Percona Server for MySQL adopted in place of its own, as it usually does remain as close to upstream as possible. The following approach is useful for cases where there is a query overloading one particular CPU while other cores are performing normally. For cases where it is a general CPU usage issue, different methods can be used, such as the one in this other blog post Reducing High CPU on MySQL: A Case Study.

主要意思是针对定位CPU的问题,Percona增加了对通过信息的TID列将processlist ID映射到OS线程ID的支持,而MySQL在5.7版本后在 PERFORMANCE_SCHEMA.THREADS表加了一个THREAD_OS_ID新列来实现,以下方法适用于在其他内核正常运行时,某个特定CPU的查询过载的情况。


find out which session is using the most CPU resources in my database?

1、定位线程

pidstat -t -p <mysqld_pid> 1  5

通过该命令我们可以定位到802、4445等线程消耗了大量的CPU,这里尽量确保在pidstat的多个样本中验证消耗是恒定的。根据这些信息,我们可以登录到数据库,并使用以下查询找出哪个MySQL线程是罪魁祸首。


2、定位问题sql

select * from performance_schema.threads where thread_os_id = xx ;
select * from information_schema.`PROCESSLIST` where  id=threads.processlist_id

根据操作系统id可以到processlist表找到对应的会话,如下:


3、查看问题sql执行计划

这里对应看一下执行计划基本就可以判断当前数据库CPU为什么消耗这么高了...

至于优化的点只需要在dock建一个索引即可,这里就不介绍了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-06-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 IT牧场 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • find out which session is using the most CPU resources in my database?
  • 2、定位问题sql
  • 3、查看问题sql执行计划
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档