目录
- -- 临时方式
- # 查询是否开启慢查询日志
- show variables like '%slow_query_log%';
- # 开启慢查询日志
- set global slow_query_log = on;
-
- # 查看时间阈值
- show variables like '%long_query_time%';
- # 设置时间
- set long_query_time=1;
-
- # 查询有多少慢查询记录
- show status like 'slow_queries';
- # 删除慢查询日志
- rm ***.log
- # 使用指令来重新生成
- 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秒,超出此设定的sql即被记录为慢查询日志
- log_output=FILE
根目录下执行,mysqldumpslow -help; 查看相关指令用法。
- # 按照时间排序查看慢查询日志前5条
- mysqldumpslow -s t -t 5 /usr/local/mysql/data/KanlinadeMacBook-Pro-slow.log
- # 查看是否开启
- show variables like 'profiling';
- #设置开启
- set profiling='ON';
- # 查看最近查询语句
- show profiles;
- # 查看指定查询语句具体执行成本
- show profile cpu,block io for quert 1;
EXPLAIN 语句
或
DESCRIBE 语句
- # 开启trace并设置格式为JSON
- set optimizer_trace="enabled=on",end_markers_in_json=on;
- set optimizer_trace_max_mem_size=1000000;
可以写增删改查语句
- # 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'