MySQL中怎么分析性能?
MySQL中主要有4种方式可以分析数据库性能,分别是慢查询日志,profile,Com_xxx和explain。
慢查询日志
先用下面命令查询慢查询日志是否开启,
show variables like 'slow_query_log';# 一般默认都是以下结果
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| slow_query_log | OFF |
+----------------+-------+
- 若结果为
ON,表示慢查询日志已开启;若为OFF,则需要手动开启。但是一般慢查询日志是默认不开启的,需要手动开启,因为需要指定指标,也就是多慢的SQL才算慢SQL。
临时开启(重启 MySQL 后失效):
# 开启慢查询日志
set global slow_query_log = 'ON';
# 设置一个时间,超过这个时间的查询都会被认为是慢查询,会记录到慢查询日志里,单位是秒(s)
set global long_query_time = 2;
永久开启:
linux环境下只需要改一下/etc/my.cnf配置文件,在里面加入如下两行配置
# 0:关闭慢查询日志 1:开启慢查询日志
slow_query_log = 1
# 指定日志文件路径(可选,不选则有默认路径)
slow_query_log_file = /var/log/mysql/slow.log
# 设置一个时间,超过这个时间的查询都会被认为是慢查询,会记录到慢查询日志里,单位是秒(s)
long_query_time = 2
# 是否记录未使用索引的查询(1表示开启,0表示关闭,默认关闭)
log_queries_not_using_indexes = 1
关键是参数【slow_query_log】和【long_query_time】一定要设置,配置完毕保存后然后使用【systemctl restart mysqld】在Linux命令行重启MySQL即可。此时慢查询的日志会记录到文件里,如果没有配置路径,使用到了默认路径,可以查询一下文件位置:
SHOW VARIABLES LIKE 'slow_query_log_file';# 得到结果可能如下
+---------------------+-------------------------------+
| Variable_name | Value |
+---------------------+-------------------------------+
| slow_query_log_file | /var/lib/mysql/hostname-slow.log |
+---------------------+-------------------------------+
然后去指定目录直接查看log文件即可。
profile
使用下列命令查看profiling是否开启
show variables like 'profiling';# 默认是关闭的,一般查询结果如下
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | OFF |
+---------------+-------+
需要手动开启。
临时开启profiling(重启 MySQL 后失效):
在SQL执行窗口设定参数
set profiling = 1;
永久开启:
在/etc/my.cnf文件中加入如下配置
profiling = 1
要记得修改过/etc/my.cnf文件以后要重启mysql。
此时随便执行几条sql,然后再来查询一下profile。
# 此时为了测试我创建了一个表
# 执行下面几条查询
select * from test where id = 2;
select * from test where id = 1;
select * from test;# 执行下行语句,查询Query记录
show profiles;
# 得到如下结果,Query列是查询语句,Duration是执行消耗的时间,Query_ID是记录ID
+----------+------------+------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------------+
| 1 | 0.00029275 | select * from test where id = 2 |
| 2 | 0.00022375 | select * from test where id = 1 |
| 3 | 0.00020425 | select * from test |
+----------+------------+------------------------------------+# 如果想要对某一条SQL进行分析,比如这里Query_ID为1的记录消耗时间最长,想要看一下具体情况,可以使用如下命令
show profile for query 1;# 得到如下结果
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000115 |
| Executing hook on transaction | 0.000008 |
| starting | 0.000009 |
| checking permissions | 0.000005 |
| Opening tables | 0.000037 |
| init | 0.000004 |
| System lock | 0.000007 |
| optimizing | 0.000009 |
| statistics | 0.000045 |
| preparing | 0.000011 |
| executing | 0.000009 |
| end | 0.000002 |
| query end | 0.000002 |
| waiting for handler commit | 0.000007 |
| closing tables | 0.000007 |
| freeing items | 0.000010 |
| cleaning up | 0.000007 |
+--------------------------------+----------+# 可以看到开始时间,执行时间,打开表的时间,优化时间,准备时间,关闭表的时间等参数
# 如果SQL查询很慢的话则可以从这里分析原因
Com_%
# 执行下列命令
show status like 'Com_%'; # 得到结果格式如下
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| Com_admin_commands | 0 |
| Com_assign_to_keycache | 0 |
| Com_alter_db | 0 |
| Com_alter_event | 0 |
| Com_alter_function | 0 |
| Com_alter_instance | 0 |
| Com_alter_procedure | 0 |
| Com_alter_resource_group | 0 |
| Com_alter_server | 0 |
| Com_alter_table | 0 |
| Com_alter_tablespace | 0 |
| Com_alter_user | 0 |
| Com_alter_user_default_role | 0 |
| Com_analyze | 0 |
| Com_begin | 0 |
| Com_binlog | 0 |
| Com_call_procedure | 0 |
| Com_change_db | 1 |
| Com_change_master | 0 |
| Com_change_repl_filter | 0 |
| Com_change_replication_source | 0 |
| Com_check | 0 |
| Com_checksum | 0 |
| Com_clone | 0 |
| Com_commit | 0 |
| Com_create_db | 0 |
| Com_create_event | 0 |
| Com_create_function | 0 |
| Com_create_index | 0 |
| Com_create_procedure | 0 |
| Com_create_role | 0 |
| Com_create_server | 0 |
| Com_create_table | 0 |
| Com_create_resource_group | 0 |
| Com_create_trigger | 0 |
| Com_create_udf | 0 |
| Com_create_user | 0 |
| Com_create_view | 0 |
| Com_create_spatial_reference_system | 0 |
| Com_dealloc_sql | 0 |
| Com_delete | 0 |
| Com_delete_multi | 0 |
| Com_do | 0 |
| Com_drop_db | 0 |
| Com_drop_event | 0 |
| Com_drop_function | 0 |
| Com_drop_index | 0 |
| Com_drop_procedure | 0 |
| Com_drop_resource_group | 0 |
| Com_drop_role | 0 |
| Com_drop_server | 0 |
| Com_drop_spatial_reference_system | 0 |
| Com_drop_table | 0 |
| Com_drop_trigger | 0 |
| Com_drop_user | 0 |
| Com_drop_view | 0 |
| Com_empty_query | 0 |
| Com_execute_sql | 0 |
| Com_explain_other | 0 |
| Com_flush | 0 |
| Com_get_diagnostics | 0 |
| Com_grant | 0 |
| Com_grant_roles | 0 |
| Com_ha_close | 0 |
| Com_ha_open | 0 |
| Com_ha_read | 0 |
| Com_help | 0 |
| Com_import | 0 |
| Com_insert | 0 |
| Com_insert_select | 0 |
| Com_install_component | 0 |
| Com_install_plugin | 0 |
| Com_kill | 0 |
| Com_load | 0 |
| Com_lock_instance | 0 |
| Com_lock_tables | 0 |
| Com_optimize | 0 |
| Com_preload_keys | 0 |
| Com_prepare_sql | 0 |
| Com_purge | 0 |
| Com_purge_before_date | 0 |
| Com_release_savepoint | 0 |
| Com_rename_table | 0 |
| Com_rename_user | 0 |
| Com_repair | 0 |
| Com_replace | 0 |
| Com_replace_select | 0 |
| Com_reset | 0 |
| Com_resignal | 0 |
| Com_restart | 0 |
| Com_revoke | 0 |
| Com_revoke_all | 0 |
| Com_revoke_roles | 0 |
| Com_rollback | 0 |
| Com_rollback_to_savepoint | 0 |
| Com_savepoint | 0 |
| Com_select | 8 |
| Com_set_option | 1 |
| Com_set_password | 0 |
| Com_set_resource_group | 0 |
| Com_set_role | 0 |
| Com_signal | 0 |
| Com_show_binlog_events | 0 |
| Com_show_binlogs | 0 |
| Com_show_charsets | 0 |
| Com_show_collations | 0 |
| Com_show_create_db | 0 |
| Com_show_create_event | 0 |
| Com_show_create_func | 0 |
| Com_show_create_proc | 0 |
| Com_show_create_table | 0 |
| Com_show_create_trigger | 0 |
| Com_show_databases | 2 |
| Com_show_engine_logs | 0 |
| Com_show_engine_mutex | 0 |
| Com_show_engine_status | 0 |
| Com_show_events | 0 |
| Com_show_errors | 0 |
| Com_show_fields | 1 |
| Com_show_function_code | 0 |
| Com_show_function_status | 0 |
| Com_show_grants | 0 |
| Com_show_keys | 0 |
| Com_show_master_status | 0 |
| Com_show_open_tables | 0 |
| Com_show_plugins | 0 |
| Com_show_privileges | 0 |
| Com_show_procedure_code | 0 |
| Com_show_procedure_status | 0 |
| Com_show_processlist | 0 |
| Com_show_profile | 5 |
| Com_show_profiles | 1 |
| Com_show_relaylog_events | 0 |
| Com_show_replicas | 0 |
| Com_show_slave_hosts | 0 |
| Com_show_replica_status | 0 |
| Com_show_slave_status | 0 |
| Com_show_status | 2 |
| Com_show_storage_engines | 0 |
| Com_show_table_status | 0 |
| Com_show_tables | 2 |
| Com_show_triggers | 0 |
| Com_show_variables | 3 |
| Com_show_warnings | 0 |
| Com_show_create_user | 0 |
| Com_shutdown | 0 |
| Com_replica_start | 0 |
| Com_slave_start | 0 |
| Com_replica_stop | 0 |
| Com_slave_stop | 0 |
| Com_group_replication_start | 0 |
| Com_group_replication_stop | 0 |
| Com_stmt_execute | 0 |
| Com_stmt_close | 0 |
| Com_stmt_fetch | 0 |
| Com_stmt_prepare | 0 |
| Com_stmt_reset | 0 |
| Com_stmt_send_long_data | 0 |
| Com_truncate | 0 |
| Com_uninstall_component | 0 |
| Com_uninstall_plugin | 0 |
| Com_unlock_instance | 0 |
| Com_unlock_tables | 0 |
| Com_update | 0 |
| Com_update_multi | 0 |
| Com_xa_commit | 0 |
| Com_xa_end | 0 |
| Com_xa_prepare | 0 |
| Com_xa_recover | 0 |
| Com_xa_rollback | 0 |
| Com_xa_start | 0 |
| Com_stmt_reprepare | 0 |
| Compression | OFF |
| Compression_algorithm | |
| Compression_level | 0 |
+-------------------------------------+-------+# 重点查看4个参数的值,Com_insert,Com_delete,Com_update,Com_select的参数。因为我没有执行增删改操作,所以都是0,刚刚又查询了几次记录,这边的Com_select已经到8了,代表当前已经执行过8次select操作,0次insert,0次delete,0次update。
在需要分析增删改查操作到底是增删改比较多还是查询比较多的时候可以使用这个方式查询相关记录的执行情况,分析某个业务到底是查询比较多呢还是更新比较多,从而可以更好地对系统架构进行把控。
explain
# 对需要执行的sql分析执行计划,假如要分析下面这条查询语句
select * from tb_user where id=1;# 语法如下
explain select * from test where id=1;
# 其实就是在查询语句前加上explain关键字,insert,update和delete语句前也可以加上进行分析执行计划
# 得到结果格式如下+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | tb_user | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
# 需要关注id列
# 相同的 id 表示同一查询块。
# id 越大,执行优先级越高。
# id 为 NULL 表示是 UNION 结果的合并操作。
# -----------------------------------------------------
# 需要关注type列,其中type列的介绍和性能如下(性能从高到低排列)
# NULL:直接查询,不进行表操作,例如select 1 + 1;
# system:表中只有一行数据(系统表)。
# const:通过主键或唯一索引查找一行数据。
# eq_ref:通过唯一索引关联表(多表 JOIN 时,每行只匹配一行)。
# ref:通过非唯一索引查找数据。
# range:使用索引范围扫描。
# index:全索引扫描(扫描索引树,不访问数据行)。
# ALL:全表扫描(性能最差)。
# -----------------------------------------------------
# 需要关注possible_keys列和key列
# possible_keys代表可能用到的索引,key就是实际用到的索引,从这里可以分析索引是不是没有用到或者失效了
# 优化的时候要尽量让没有使用到索引的语句使用索引
# -----------------------------------------------------
# 需要关注key_len
# 如果用到了单列索引,则key_len是一个固定值
# 如果用到了联合索引,key_len的值可能会因为部分索引失效而导致key_len的值不一样,可以通过这一列判断联合索引是否全部生效。
# -----------------------------------------------------
# 需要关注rows列,记录的是MySQL预估需要扫描的行数。
# 行数越少,性能越好,如果值很大,可能需要优化索引或查询条件。
# -----------------------------------------------------
# 需要关注filtered列
# filtered= Server层过滤后的行数/存储引擎层返回的行数 ×100%
# 值越小,说明存储引擎层已经过滤了更多不满足条件的数据,Server 层只需处理少量数据。
# -----------------------------------------------------
# 重点关注Extra列,其中可能出现的值如下:
# Using where:使用了 WHERE 条件过滤数据。
# Using index:使用了覆盖索引(无需回表)。
# Using temporary:使用了临时表(性能较差)。
# Using filesort:使用了文件排序(性能较差)。
# Using join buffer:使用了 JOIN 缓冲区(多表 JOIN 时)。
# Impossible WHERE:WHERE 条件永远为假(无结果)。
# 需要注意尽可能避免Using temporary和Using filesort,以及Impossible WHERE。
相关文章:
MySQL中怎么分析性能?
MySQL中主要有4种方式可以分析数据库性能,分别是慢查询日志,profile,Com_xxx和explain。 慢查询日志 先用下面命令查询慢查询日志是否开启, show variables like slow_query_log;# 一般默认都是以下结果 ---------------------…...
阿里云对象存储教程
搜“对象存储->免费试用” 选择你的心仪产品,我使用的是第一个 创建后获得三个实例: 点击右上角自己的账号可以进入到AccessKey管理界面 回到对象存储控制台创建Bucket实例 在以下文件中替换自己Bucket的信息即可美美使用~ package com.kitty.blog…...
【Node.js入门笔记10---http 模块】
Node.js入门笔记10 Node.js---http 模块一、核心功能0.学习http的前提1. 创建 HTTP 服务器2. 处理请求和响应 二、进阶用法1. 路由管理2. 处理 POST 请求3. 中间件模式 三、常见场景四、错误处理与安全五、对比 http 与 Express六、工具库推荐: Node.js—http 模块 …...
深拷贝在 JavaScript 中的几种实现方式对比
深拷贝在 JavaScript 中的几种实现方式对比 1. JSON 序列化法2. 结构化克隆(structuredClone)原理与使用 3. 自定义深拷贝函数原理与使用 性能对比与选择建议性能比较 综合建议:示例代码整合总结 在开发过程中,我们经常需要对对象…...
实验11 机器学习-贝叶斯分类器
实验11 机器学习-贝叶斯分类器 一、实验目的 (1)理解并熟悉贝叶斯分类器的思想和原理; (2)熟悉贝叶斯分类器的数学推导过程; (3)能运用贝叶斯分类器解决实际问题并体会算法的效果&a…...
Delta Lake 解析:架构、数据处理流程与最佳实践
Delta Lake 是一个基于 Apache Spark 的开源存储层,主要解决传统数据湖(Data Lake)缺乏 ACID 事务、数据一致性和性能优化的问题,使大数据处理更加可靠、高效。从本质上讲,它让数据湖具备了数据仓库的结构化管理能力&a…...
OpenHarmony子系统开发 - 电池管理(二)
OpenHarmony子系统开发 - 电池管理(二) 五、充电限流限压定制开发指导 概述 简介 OpenHarmony默认提供了充电限流限压的特性。在对终端设备进行充电时,由于环境影响,可能会导致电池温度过高,因此需要对充电电流或电…...
hive 数据简介
Hive介绍 1)Hive简介 Hive是基于Hadoop的一个数据仓库工具,用于结构化数据的查询、分析和汇总。Hive提供类SQL查询功能,它将SQL转换为MapReduce程序。 Hive不支持OLTP,Hive无法提供实时查询。 2)Hive在大数据生态环境…...
Win32桌面编程:ACLUI.DLL,EditSecurity(IntPtr hwndOwner, ISecurityInformation psi)
在Windows编程中,我们通常需要借助通用对话框的力量,今天我们就聊一下“安全属性表”通用对话框的使用心得。 当我们调用EditSecurity函数时: 1.EditSecurity将调用ISecurityInformation中的GetObjectInformation函数 在编写 ISecurityInf…...
数据分析异步进阶:aiohttp与Asyncio性能提升
一、时间轴呈现方案进程 2023-04-01:需求确认 确定目标:使用aiohttp与Asyncio提升采集性能,目标采集今日头条网站的新闻数据(标题、内容、时间等)。同时要求在程序中加入代理IP、Cookie和UserAgent的设置,…...
《AI大模型趣味实战 》第8集:多端适配 个人新闻头条 基于大模型和RSS聚合打造个人新闻电台(Flask WEB版) 2
《AI大模型趣味实战 》第8集:多端适配 个人新闻头条 基于大模型和RSS聚合打造个人新闻电台(Flask WEB版) 2 摘要 本文末尾介绍了如何实现新闻智能体的方法。在信息爆炸的时代,如何高效获取和筛选感兴趣的新闻内容成为一个现实问题。本文将带领读者通过P…...
低配电脑畅玩《怪物猎人:荒野》,ToDesk云电脑优化从30帧到144帧?
《怪物猎人:荒野(Monster Hunter Wilds)》自2025年正式发售以来已取得相当亮眼的成绩,仅用三天时间便轻松突破800万销量,目前顺利蝉联周榜冠军;凭借着开放世界的宏大场景和丰富的狩猎玩法,该游戏…...
Leetcode刷题笔记1 图论part03
卡码网 101 孤岛总面积 from collections import deque directions [[0, 1], [1, 0], [0, -1], [-1, 0]] count 0def main():global countn, m map(int, input().split())grid []for _ in range(n):grid.append(list(map(int, input().split())))for i in range(n):if gri…...
【模拟面试】计算机考研复试集训(第十一天)
文章目录 前言一、专业面试1、什么是面向对象编程?2、软件工程的主要模型有哪些?3、Cache和寄存器的区别4、卷积层有哪些参数,它们代表什么?5、你有读博的打算吗?6、你的师兄/姐临近毕业,仍做不出成果&…...
查看自己的公有ip
IP 地址 112.3.88.1** 是一个 公有 IP 地址,而不是私有 IP 地址。 公有 IP 地址 vs 私有 IP 地址 公有 IP 地址: 用于在互联网上唯一标识设备。由互联网服务提供商(ISP)分配。可以在全球范围内路由和访问。例如:112.3.88.156、8.8…...
【js逆向入门】图灵爬虫练习平台 第九题
地址:aHR0cHM6Ly9zdHUudHVsaW5ncHl0b24uY24vcHJvYmxlbS1kZXRhaWwvOS8 f12进入了debugger,右击选择一律不在此处暂停, 点击继续执行 查看请求信息 查看载荷,2个加密参数,m和tt 查看启动器,打上断点 进来 往…...
NET6 WebApi第5讲:中间件(源码理解,俄罗斯套娃怎么来的?);Web 服务器 (Nginx / IIS / Kestrel)、WSL、SSL/TSL
一、NET6的启动流程 区别: .NET6 WebApi第1讲:VSCode开发.NET项目、区别.NET5框架【两个框架启动流程详解】_vscode webapi-CSDN博客 2、WebApplicationBuilder:是NET6引入的一个类,是建造者模式的典型应用 1>建造者模式的…...
Nginx及前端部署全流程:初始化配置到生产环境部署(附Nginx常用命令)
nginx&前端从初始化配置到部署(xshell) 前言下载nginx前端打包与创建具体文件夹路径配置nginx.nginx.conf文件配置项内容 配置nginx.service文件配置项内容 启动nginx常用nginx命令 前言 目标:在xshell中部署前端包。 第一步:…...
python 实现一个简单的window 任务管理器
import tkinter as tk from tkinter import ttk import psutil# 运行此代码前,请确保已经安装了 psutil 库,可以使用 pip install psutil 进行安装。 # 由于获取进程信息可能会受到权限限制,某些进程的信息可能无法获取,代码中已经…...
【AI模型】深度解析:DeepSeek的联网搜索的实现原理与认知误区
一、大模型的“联网魔法”:原来你是这样上网的! 在人工智能这个舞台上,大模型们可是妥妥的明星。像DeepSeek、QWen这些大模型,个个都是知识渊博的“学霸”,推理、生成文本那叫一个厉害。不过,要是论起上网…...
【xiaozhi赎回之路-2:语音可以自己配置就是用GPT本地API】
固件作用 打通了网络和硬件的沟通 修改固件实现【改变连接到小智服务器的】 回答逻辑LLM自定义 自定义了Coze(比较高级,自定义程度比较高,包括知识库,虚拟脚色-恋人-雅思老师-娃娃玩具{可能需要使用显卡对开源模型进行微调-产…...
WX小程序
下载 package com.sky.utils;import com.alibaba.fastjson.JSONObject; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.Cl…...
JavaScript案例0322
以下是一些涵盖不同高级JavaScript概念和应用的案例,每个案例都有详细解释: 案例1:实现 Promise/A 规范的手写 Promise class MyPromise {constructor(executor) {this.state pending;this.value undefined;this.reason undefined;this.o…...
Spring boot 3.4 后 SDK 升级,暨 UI API/MCP 计划
PS 写这篇文章后看到 A Deep Dive Into MCP and the Future of AI Tooling | Andreessen HorowitzWe explore what MCP is, how it changes the way AI interacts with tools, what developers are already building, and the challenges that still need solving. https://a1…...
大数据学习(78)-spark streaming与flink
🍋🍋大数据学习🍋🍋 🔥系列专栏: 👑哲学语录: 用力所能及,改变世界。 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言📝支持一…...
2.企业级AD活动目录架构与设计原则实战指南
一、企业级AD架构核心组件解析 1.1 多域森林架构设计 核心概念: 单域模型:适用于中小型企业(<5万用户) 多域模型:满足跨国/多部门隔离需求 森林(Forest):安全信任边界&#x…...
Linux下JDK1.8安装配置
目录 1.下载完上传到Linux系统中 2.解压JDK压缩包 3.配置JDK环境变量 4.设置环境变量生效 5.查看环境变量是否配置成功 官网下载地址:Java Downloads | Oracle 1.下载完上传到Linux系统中 2.解压JDK压缩包 tar -zxvf jdk-8u151-linux-x64.tar.gz -C /usr/local (解压…...
Python OCR文本识别详细步骤及代码示例
光学字符识别(OCR)是将图像中的文字转换为可编辑文本的技术。在Python中,我们可以利用多种库实现OCR功能。本文将详细介绍使用Tesseract和EasyOCR进行文本识别的步骤,并提供完整的代码示例。 一、OCR简介 OCR(Optical…...
OpenCV 基础模块 Python 版
OpenCV 基础模块权威指南(Python 版) 一、模块全景图 plaintext OpenCV 架构 (v4.x) ├─ 核心层 │ ├─ core:基础数据结构与操作(Mat/Scalar/Point) │ └─ imgproc:图像处理流水线(滤…...
华为HCIE网络工程师培训选机构攻略
从 官方授权机构 到 性价比黑马,结合价格、师资、通过率等维度,为你筛选出最适合的培训方案。 一、华为官方授权机构(优先推荐) 华为官方授权机构拥有 真机实验环境考官级讲师,适合预算充足、追求高通过率的学员。 机…...
