MySql:优化表查询

是否有可能找出“优化”命令能够对给定表格进行碎片整理多less? 我只是想事先了解这些信息。

我对这个答案有了一些第二个想法:-)其实就像查找价值一样简单 – 其全部在information_schema.tables中! 查找名为data_free的字段,它具有正确的值,并且在我所做的示例中也行得通:

use test; create table test1 as select * from information_schema.tables; alter table test1 engine = myisam; -- not even necessary select * from information_schema.tables where table_name = 'test1'\G delete from test1 limit 10; select * from information_schema.tables where table_name = 'test1'\G analyze table test1; select * from information_schema.tables where table_name = 'test1'\G optimize table test1; select * from information_schema.tables where table_name = 'test1'\G 

information_schema.tables始终显示正确的数据长度,因此您可以将data_length字段与所需数据长度(avg_row_length * table_rows)的粗略估计进行比较,但是只有使用分析表更新了统计信息后:

 analyze table 'TABLE_TO_LOOK_UP'; -- to get row count etc. right SELECT table_name, concat( round((data_length - (avg_row_length * table_rows)) / 1024 / 1024, 2) , 'M' ) very_theoretical_size_difference_in_MB FROM information_schema.tables WHERE table_name = 'TABLE_TO_LOOK_UP';