互联网服务MySQL

mysql中的索引膨胀率,如何查看?

mysql中的索引膨胀率,如何查看?怎么判断一个索引是否因为碎片率过高,需要重建呢 ?pg中有索引密度等几个维度,mysql中如何判断呢?显示全部

mysql中的索引膨胀率,如何查看?

怎么判断一个索引是否因为碎片率过高,需要重建呢 ?

pg中有索引密度等几个维度,mysql中如何判断呢?

收起
参与5

返回hbhe0316的回答

hbhe0316hbhe0316数据库管理员IBM

-- 列出所有已经产生碎片的表
select table_schema db, table_name, round(data_free/1024/1024, 2) data_free, engine,table_rows, round((data_length+index_length)/1024/1024,2) length
from information_schema.tables where table_schema not in ('information_schema', 'mysql') and data_free > 0;

-- 查询并计算碎片率
select table_schema db, table_name, engine,table_rows, round(data_free/1024/1024, 2) data_free_M, round((data_length+index_length)/1024/1024,2) length_M , round(data_free/(data_free + data_length+index_length),2) rate
from information_schema.tables where table_schema not in ('information_schema', 'mysql') and data_free > 0 order by data_free_M desc ,rate desc;

--查看某张表的碎片率
mysql> show table status like 't_exception_log202005';

1) MySQL官方建议不要经常(每小时或每天)进行碎片整理,一般根据实际情况,只需要每周或者每月整理一次即可;
2) OPTIMIZE TABLE只对MyISAM,BDB和InnoDB表起作用,尤其是MyISAM表的作用最为明显。此外,并不是所有表都需要进行碎片整理,一般只需要对包含上述可变长度的文本数据类型的表进行整理即可;
3) 在OPTIMIZE TABLE 运行过程中,MySQL会锁定表;

IT分销/经销 · 2023-08-01
浏览556

回答者

hbhe0316
数据库管理员IBM
擅长领域: 服务器数据库中间件

hbhe0316 最近回答过的问题

回答状态

  • 发布时间:2023-08-01
  • 关注会员:2 人
  • 回答浏览:556
  • X社区推广