查看mysql表的碎片率:
SELECT TABLE_SCHEMA
,TABLE_NAME
,ENGINE
,ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024), 2) AS SIZE_MB
,ROUND(DATA_FREE/1024/1024,2) AS FREE_SIZ_MB
FROM information_schema.TABLES
WHERE DATA_FREE >=5*1024*1024
ORDER BY FREE_SIZ_MB DESC;
Myisam引擎使用下面语句优化表:
optimize table XXX
https://editor.csdn.net/md/?articleId=120748477
关于innodb的表优化,看了官方文档,要想optimize语句生效,有一些前提条件.
https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
查看索引使用情况:
SELECT object_type, object_schema, object_name, index_name, count_star, count_read, COUNT_FETCH FROM PERFORMANCE_SCHEMA.table_io_waits_summary_by_index_usage WHERE object_schema = "xxx" AND object_name = "xxx"