MysqL如何查询MysqL性能?分享常用的20个MysqL性能查询常用命令写法供你参考使用。
1、查看每个客户端IP过来的连接消耗了多少资源
MysqL> select * from host_summary;
2、查看某个数据文件上发生了多少IO请求
MysqL> select * from io_global_by_file_by_bytes;
3、查看每个用户消耗了多少资源
MysqL> select * from user_summary;
4、查看总共分配了多少内存
MysqL> select * from memory_global_total;
5、数据库查看当前连接情况。
MysqL> select host, current_connections, statements from host_summary;
6、查看当前正在执行的sql和执行show full processlist的效果相当。
MysqL> select conn_id, user, current_statement, last_statement from session;
7、数据库中哪些sql被频繁执行?执行下面命令查询TOP 10最热sql。
MysqL> select db,exec_count,query from statement_analysis order by exec_count desc limit 10;
8、哪个文件产生了最多的IO,读多,还是写的多?
MysqL> select * from io_global_by_file_by_bytes limit 10;
9、哪个表上的IO请求最多?
MysqL> select * from io_global_by_file_by_bytes where file like ‘%ibd’ order by total desc limit 10;
10、哪个表被访问的最多?先访问statement_analysis,根据热门sql排序找到相应的数据表。哪些语句延迟比较严重?查看statement_analysis中avg_latency的最高的sql。
MysqL> select * from statement_analysis order by avg_latency desc limit 10;
11、或者查看statements_with_runtimes_in_95th_percentile视图
MysqL> select * from statements_with_runtimes_in_95th_percentile;
12、哪些sql执行了全表扫描或执行了排序操作?
MysqL> select * from statements_with_sorting;
MysqL> select * from statements_with_full_table_scans;
13、哪些sql语句使用了临时表,又有哪些用到了磁盘临时表?查看statement_analysis中哪个sql的tmp_tables 、tmp_disk_tables值大于0即可。
MysqL> select db, query, tmp_tables, tmp_disk_tables from statement_analysis where tmp_tables>0 or tmp_disk_tables >0 order by (tmp_tables+tmp_disk_tables) desc limit 20;
14、也可以查看statements_with_temp_tables视图
MysqL> select * from statements_with_temp_tables\G
15、哪个表占用了最多的buffer pool?
MysqL> select * from innodb_buffer_stats_by_table order by allocated desc limit 10;
16、每个库(database)占用多少buffer pool?
MysqL> select * from innodb_buffer_stats_by_schema order by allocated desc limit 10;
17、每个连接分配多少内存?利用session表和memory_by_thread_by_current_bytes分配表进行关联查询。
MysqL> select b.user, current_count_used, current_allocated, current_avg_alloc, current_max_alloc, total_allocated,current_statement from memory_by_thread_by_current_bytes a, session b where a.thread_id = b.thd_id;18,MysqL自增长字段的最大值和当前已经使用到的值?
MysqL> select * from schema_auto_increment_columns;
MysqL> select * from schema_index_statistics;
20、MysqL有哪些冗余索引和无用索引?
MysqL> select * from schema_redundant_indexes;
MysqL> select * from schema_unused_indexes;
21、MysqL内部有多个线程在运行?MysqL内部的线程类型及数量。
MysqL> select user, count(*) from processlist group by user;