当前位置:网站首页>MySQL - slow query log
MySQL - slow query log
2022-08-02 07:50:00 【kk_lina】
目录
4.3、The statement outputs the role of each column
一、相关参数
-- 临时方式
# 查询是否开启慢查询日志
show variables like '%slow_query_log%';
# 开启慢查询日志
set global slow_query_log = on;
# 查看时间阈值
show variables like '%long_query_time%';
# 设置时间
set long_query_time=1;
# How many slow query records the query has
show status like 'slow_queries';
# 删除慢查询日志
rm ***.log
# Use the command to regenerate
mysqladmin -uroot -p flush-logs slow
-- 永久方式
[mysql]
slow_query_log=ON # 开启慢查询日志
slow_query_log_file=/var/lib/mysql/***.log # 慢查询日志的目录和文件名信息 /usr/local/mysql/data/KanlinadeMacBook-Pro-slow.log
long_query_time=3 #设置慢查询的阈值为3秒,beyond this settingsqlThat is, it is recorded as the slow query log
log_output=FILE二、分析工具 mysqldumpslow
根目录下执行,mysqldumpslow -help; See related command usage.
# Before viewing the slow query log in chronological order5条
mysqldumpslow -s t -t 5 /usr/local/mysql/data/KanlinadeMacBook-Pro-slow.log 三、查看执行成本 show profile
# 查看是否开启
show variables like 'profiling';
#设置开启
set profiling='ON';
# View recent queries
show profiles;
# View the specific execution cost of a specified query statement
show profile cpu,block io for quert 1;四、分析查询语句:EXPLAIN
4.1、能做什么
- 表的读取顺序;
- 数据读取操作的操作类型;
- 哪些索引可以使用;
- 哪些索引实际被使用;
- 表之间的引用;
- 每张表有多少行被优化器查询.
4.2、基本语法
EXPLAIN 语句
或
DESCRIBE 语句
4.3、The statement outputs the role of each column

- table:Each record queried corresponds to a single table;
- id:在一个大的查询语句中每个select关键字都对应一个唯一的id;
- select_type:What role does small query play in big query;
- type:mysqlThe access method to the table when executing the query:system(When there is only one piece of data in the table and the storage engine statistics for the table are accurate),const(The primary key or unique secondary index is matched against a constant),eq_ref(连接查询时,The driven table is accessed by matching the value of the primary key or unique secondary index column),ref(It is queried by performing equal value matching between ordinary secondary index columns and constants),index_merge(It can be used in some scenarios when the single-table access method is usedIntersection、Union、Sort-Union这三种索引合并的方式来执行查询),unique_subquery(将in查询转换成exists,而且子查询可以使用到主键进行等值匹配),range(范围查询),index(使用索引覆盖,但需要扫描全部索引记录时),all(全表扫描).
- possible_keys:可能使用到的索引;
- key:真实使用的索引;
- key_len:实际使用到的索引长度(字节数),值越大越好
- ref:与索引列进行等值匹配的对象信息;
4.4、小结
- EXPLAIN不考虑各种Cache;
- EXPLAIN不能显示MySQL在执行查询时所作的优化工作;
- EXPLAIN不会告诉你关于触发器、存储过程的信息或者用户自定义函数对查询的影响;
- 部分统计信息是估算的,并非精确值.
4.5、四种输出格式
- 传统格式:表格形式
- JSON格式:EXPLAIN FORMAT=JSON,One more form of measuring how good a plan is
- TREE格式:According to the query order and relationship of each part
- 可视化输出:mysql workbench
五、分析优化器执行计划:trace
5.1、相关语句
# 开启trace并设置格式为JSON
set optimizer_trace="enabled=on",end_markers_in_json=on;
set optimizer_trace_max_mem_size=1000000;You can write CRUD statements
# mysql如何执行
select * from information_schema.optimizer_trace \G
# 查询冗余索引
select * from sys.schema_redundant_indexes;
# 查询未使用过的索引
select * from sys.schema_unused_indexes;
# 查询索引的使用情况
select index_name,rows_selected,rows_inserted,rows_updated,rows_deleted
from sys.schema_index_statistics where table_schema='dbname';
# 查询表的访问量
select table_schema,table_name,sum(io_read_requests+io_write_requests) as io
from sys.innodb_buffer_stats_by_table
order by allocated
limit 10
# 查询占用bufferPool较多的表
select object_schema,object_name,allocated,DATA
from sys.innodb_buffer_stats_by_table
order by allocated
limit 10
# 查看表的全表扫描情况
select * from sys.statements_with_full_table_scans where db='dbname'
边栏推荐
猜你喜欢

OC-error prompt
![WebGPU 导入[2] - 核心概念与重要机制解读](/img/8f/195a3b04912d8872d025df58ab5960.png)
WebGPU 导入[2] - 核心概念与重要机制解读

你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑

2022.07.31(LC_6132_使数组中所有元素都等于零)

59:第五章:开发admin管理服务:12:MongoDB的使用场景;(非核心数据,数据量比较大的非核心数据,人脸照片等隐私的小文件;)

海缆探测仪TSS350(二)

Agile, DevOps and Embedded Systems Testing

LeetCode 2360. The longest cycle in a graph

MySQL-FlinkCDC-Hudi实时入湖

实例028:递归求等差数列
随机推荐
吃透Chisel语言.31.Chisel进阶之通信状态机(三)——Ready-Valid接口:定义、时序和Chisel中的实现
论文阅读 (64):Weakly-supervised Video Anomaly Detection with Robust Temporal Feature Magnitude Learning
【红队】ATT&CK - 创建或修改系统进程实现持久化(更新ing)
责任链模式(Chain Of Responsibility)
_2_顺序表
交换部分 VLAN
使用hutool做本地缓存的工具类
SQL执行顺序
结构体大小计算--结构体内存对齐
(2022牛客多校五)C-Bit Transmission(思维)
自然语言处理 文本预处理(上)(分词、词性标注、命名实体识别等)
CSRF-跨站请求伪造-相关知识
张驰课堂:六西格玛测量系统的误差分析与判定
2022.07.31(LC_6132_使数组中所有元素都等于零)
新产品立大功 伟世通第二季度营收双增
MPLS的相关技术
2022夏暑假每日一题(六)
敏捷、DevOps和嵌入式系统测试
(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints
LeetCode 2312. Sell Wood Blocks