MySQL 优化利器 SHOW PROFILE 的实现原理

背景

近期,我遇到了一个技术挑战:通过传输表空间的方式导入一个体积达4GB的表,整个过程耗时13分钟。通过PROFILE工具的分析,我惊讶地发现,大部分时间竟然花费在了System lock阶段。

mysql> set profiling=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> alter table sbtest2 import tablespace; Query OK, 0 rows affected (13 min 8.99 sec)
mysql> show profile for query 1; +--------------------------------+------------+ | Status | Duration | +--------------------------------+------------+ | starting | 0.000119 | | Executing hook on transaction | 0.000004 | | starting | 0.000055 | | checking permissions | 0.000010 | | discard_or_import_tablespace | 0.000007 | | Opening tables | 0.000156 | | System lock | 788.966338 | | end | 0.007391 | | waiting for handler commit | 0.000041 | | waiting for handler commit | 0.011179 | | query end | 0.000022 | | closing tables | 0.000019 | | waiting for handler commit | 0.000031 | | freeing items | 0.000035 | | cleaning up | 0.000043 | +--------------------------------+------------+ 15 rows in set, 1 warning (0.03 sec)

更令人困惑的是,在执行SQL的过程中,show processlist显示的状态也是System lock

mysql> show processlist; +----+-----------------+-----------+--------+---------+------+------------------------+---------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-----------------+-----------+--------+---------+------+------------------------+---------------------------------------+ | 5 | event_scheduler | localhost | NULL | Daemon | 818 | Waiting on empty queue | NULL | | 10 | root | localhost | sbtest | Query | 648 | System lock | alter table sbtest2 import tablespace | | 14 | root | localhost | NULL | Query | 0 | init | show processlist | +----+-----------------+-----------+--------+---------+------+------------------------+---------------------------------------+ 3 rows in set, 1 warning (0.00 sec)

这种状态显示具有很大的误导性。

接下来,我们将从SHOW PROFILE的基本用法出发,深入源码层面,分析其实现原理,并探讨案例中的表空间导入操作为何大部分耗时集中在System lock阶段。

SHOW PROFILE 的基本用法

让我们通过一个示例来了解SHOW PROFILE的用法。

# 开启 Profiling
mysql> set profiling=1; Query OK, 0 rows affected, 1 warning (0.00 sec)
# 执行需要分析的 SQL
mysql> select count(*) from slowtech.t1; +----------+ | count(*) | +----------+ | 1048576 | +----------+ 1 row in set (1.09 sec)
# 通过 show profiles 查看 SQL 对应的 Query_ID
mysql> show profiles; +----------+------------+----------------------------------+ | Query_ID | Duration | Query | +----------+------------+----------------------------------+ | 1 | 1.09378600 | select count(*) from slowtech.t1 | +----------+------------+----------------------------------+ 1 row in set, 1 warning (0.00 sec)
# 查看该 SQL 各个阶段的执行耗时情况,其中,1 是该 SQL 对应的 Query_ID
mysql> show profile for query 1; +--------------------------------+----------+ | Status | Duration | +--------------------------------+----------+ | starting | 0.000157 | | Executing hook on transaction | 0.000009 | | starting | 0.000020 | | checking permissions | 0.000012 | | Opening tables | 0.000076 | | init | 0.000011 | | System lock | 0.000026 | | optimizing | 0.000013 | | statistics | 0.000033 | | preparing | 0.000032 | | executing | 1.093124 | | end | 0.000025 | | query end | 0.000013 | | waiting for handler commit | 0.000078 | | closing tables | 0.000048 | | freeing items | 0.000076 | | cleaning up | 0.000037 | +--------------------------------+----------+ 17 rows in set, 1 warning (0.01 sec)

如果指定 all,还会输出更详细的统计信息,包括 CPU、上下文切换、磁盘IO、IPC(进程间通信)发送/接受的消息数量、

文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/4373.html

(0)
LomuLomu
上一篇 2024 年 12 月 24 日 下午9:38
下一篇 2024 年 12 月 24 日

相关推荐

  • MySQL

    阿里云社区https://developer.aliyun.com/mirror 目录 一:数据库 1.1 二: MySQL数据库基本操作 2.1 创建数据库: 2.2 使用某个数据库: 2.3 删除数据库: 2.4 查询支持的存储引擎 2.5 创建表: 2.6 查看表结构: 2.7 查看表结构详细信息: 2.8 删除表: 三:表的操作 3.1 修改表名字:…

    未分类 2025 年 1 月 11 日
    44400
  • 【GreatSQL优化器-10】find_best_ref

    【GreatSQL优化器-10】find_best_ref 一、find_best_ref介绍 GreatSQL的优化器对于join的表需要根据行数和cost来确定最后哪张表先执行哪张表后执行,这里面就涉及到预估满足条件的表数据,在keyuse_array数组有值的情况下,会用find_best_ref函数来通过索引进行cost和rows的估计,并且会找出最…

    2025 年 1 月 16 日
    60400
  • MySQL 安装部署

    概述 本文主要介绍如何在 Linux 中以 RPM 包的方式安装 MySQL 并进行相关的初始化配置,文中方案均已实践验证。 操作系统 CentOS 7.6 数据库版本 MySQL 8.4.3 LTS [!NOTE] 说明 本文主要介绍 RPM 包的安装方式,其他安装方法可查阅官网:MySQL :: MySQL 8.4 Reference Manual ::…

    未分类 2025 年 1 月 11 日
    51100
  • Python包管理不再头疼:uv工具快速上手

    Python 包管理生态中存在多种工具,如 pip、pip-tools、poetry、conda 等,各自具备一定功能。 而今天介绍的uv 是 Astral 公司推出的一款基于 Rust 编写的 Python 包管理工具,旨在成为 “Python 的 Cargo ”。 它提供了快速、可靠且易用的包管理体验,在性能、兼容性和功能上都有出色表现,为 Python…

    2024 年 12 月 31 日
    86100
  • java: JDK isn‘t specified for module ‘product-service‘问题解决

    目录 问题 解决方法 1.打开File->Project Structure… 2.将Project SDK修改为17 Oracle OpenJDK 17.0.12,并Apply,OK 问题 添加module后报错:java: JDK isn’t specified for module ‘product-service’ 查看pom.xml文件也添加了…

    2025 年 1 月 21 日
    72700

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信