当前位置:网站首页>MySql database optimization query tool
MySql database optimization query tool
2022-07-31 08:00:00 【@@Shen Nong write code】
一、MySql数据库监控工具 Profile
- Profile工具
用来统计SQL语句的执行时间. - 条件
- Profile工具
- MySql数据库
- 步骤
打开Profile
# 打开Profile set profiling = 1;执行sql语句
# 执行sql语句sql语句分析
# sql分析 show profiles;如图:

sqlThe time for each procedure of the statement execution
# sqlThe time for each procedure of the statement execution show profile for query 5;如图:

分析过程
# 分析过程 show profile all for query 5;关闭监控
set profiling = 0;如图:

缺陷
- 只能在mysql客户端对sql进行性能分析.
- 解决方案
- 使用索引 sqlThe performance of the statement is improved entirely by the index
- 使用缓存
- mysql本地缓存
- 使用webapi [IMemoryCache]
- 使用分布式缓存 [redis]
二、SqlInquire about technical tools Explain [推荐使用]
- 目的
主要用来分析sqlHow statements are optimized. - 步骤
开启 Explain
explain [sql语句]; # 例如 explain select * from Table;如图:

- 表结构说明
字段名称 备注 id sqlExecute sequence number select_type 查询类型 table 表名 partitions 分区 type 是否使用索引 possible_keys Possible index names to use key The final decision on the index name to use key_len 查询索引使用的字节数,通常是越少越好 尽量在INTAdd an index on the type ref 查询的列或常量 rows 需要扫描的行数 filtered 过滤 extra 额外的信息 - Table field properties
- select_type [查询类型]
- 单表查询:SIMPLE;
- 联合查询:PRIMARY[The first of a union querysql];UNION[联合sql];
- 子查询:PRIMARY[首先执行的sql语句];SUBQUERY[子查询];
- 关联查询:PRIMARY[首先执行的sql语句];
- DEPENDENT SUBQUERY[依赖子查询,The main table must be queried to get the results,side table dependency,不走索引,性能很慢];
- DEPENDENT UNION[跟UNION相同,But rely on the outer query results];DERIVED[临时表;用于FROM 中的子查询]
- type [是否使用索引]
- ALL:全表扫描;
- const:常量[性能最高 1对1 查询,Set to primary key or set tounqiue索引];
- ref:A field uses an index;
- eq_ref:Indexes are used in joint queries;
- range:[索引范围查找];
- index:[组合索引]
- select_type [查询类型]
- Table field properties
- The purpose of using a query plan
- Know how many rows the target data is from
- Query whether the target data uses the index
- 执行了多少sql语句
- 表结构说明
三、Sql慢查询日志分析
- The concept of slow queries
A slow query refers to a record that takes a long time to executesql语句. - 步骤
Check whether the slow query is enabled in the system
show variables like '%slow_query_log%';如图:

开启慢查询
#开启 [0:关闭 1:开启] set global slow_query_log = 1;如图:

Can be modifiedmysqlThe configuration file sets the enable of slow query、File storage path and monitoring time,并重启mysql服务
执行慢SQL语句
- 缺陷
- Concurrency is high in production environments,生成很多的sql日志,排查慢sql会很麻烦.
边栏推荐
猜你喜欢
随机推荐
CY7C68013A之LED闪烁
我开发了一个利用 Bun 执行 .ts / .js 文件的 VS Code 插件
2022.07.20_每日一题
Failure scenarios of @Transactional annotations
2022.07.12_每日一题
NK-RTU980烧写裸机程序
Jetpack Compose学习(8)——State及remeber
Leetcode952. 按公因数计算最大组件大小
7/28-7/29 期望+思维+后缀数组+ST表
波士顿房价数据集 Boston house prices dataset
Reimbursement Process | By Tianfang
2022.07.26_Daily Question
2022.07.14_每日一题
XSS详解
Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
2022.07.14_Daily Question
Spark 在 Yarn 上运行 Spark 应用程序
Ceph单节点部署
New in Visual Studio: Low Priority Builds
XSS靶场prompt.ml过关详解









