当前位置:网站首页>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'
边栏推荐
猜你喜欢
随机推荐
(2022牛客多校五)D-Birds in the tree(树形DP)
【机器学习】实验3布置:贝叶斯垃圾邮件识别
OC-NSDictionary
(2022牛客多校五)B-Watches(二分)
“蔚来杯“2022牛客暑期多校训练营4,签到题NDKHL
企业实训复现指导手册——基于华为ModelArts平台的OpenPose模型的训练和推理、基于关键点数据实现对攀爬和翻越护栏两种行为的识别、并完成在图片中只标注发生行为的人
【请教】SQL语句按列1去重来计算列2之和
海缆探测仪TSS350(二)
LeetCode 2360. The longest cycle in a graph
Splunk Field Caculated 计算字段
OC-NSNumber and NSValue are generally used for boxing and unboxing
OC-NSString
张驰课堂:六西格玛测量系统的误差分析与判定
根据一个字段的内容去更新另一个字段的数据,这样的sql语句该怎么样书写
Splunk Filed extraction 字段截取
Agile, DevOps and Embedded Systems Testing
php删除一维数组中一个值
apt & apt-get命令
CollectionUtil:一个函数式风格的集合工具
OC-NSNumber和NSValue一般用来装箱拆箱