当前位置:网站首页>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'
边栏推荐
猜你喜欢

第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】

以训辅教,以战促学 | 新版攻防世界平台正式上线运营!

MySQL-FlinkCDC-Hudi实时入湖

LeetCode 2312. 卖木头块

交换部分 VLAN

吃透Chisel语言.30.Chisel进阶之通信状态机(二)——FSMD:以Popcount为例

【请教】SQL语句按列1去重来计算列2之和

(2022牛客多校五)B-Watches(二分)

【ROS基础】map、odom、base_link、laser 的理解 及其 tf 树的理解

吃透Chisel语言.31.Chisel进阶之通信状态机(三)——Ready-Valid接口:定义、时序和Chisel中的实现
随机推荐
自然语言处理 文本预处理(上)(分词、词性标注、命名实体识别等)
【机器学习】实验2布置:基于回归分析的大学综合得分预测
跨阻放大器
概率论与数理统计
责任链模式(Chain Of Responsibility)
Splunk Filed Alias 字段改名
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】
张驰咨询:企业实施精益管理的最大障碍,只把精益作为一种工具和方法
21 days learning challenge 】 【 sequential search
spark 读取本地文件
条件构造器~wapper
Splunk Field Caculated 计算字段
【ROS基础】rosbag 的使用方法
LeetCode 2312. 卖木头块
交换部分 VLAN
倍福使用AdsRemote组件实现和C#的ADS通讯
mysql 注入
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
OC-NSDictionary
从云计算到函数计算