mysql高水位線問題:
刪除數據、
插入數據索引不連續帶來的頁內數據空洞,表實際占用空間增大
優化:
alter table table_name engine = InnoDB
相當於建立臨時表,把表刪除后,重新插入數據
原理:
InnoDB引擎只會把這個記錄標記為刪除,如果要復用,必須要插入相同id的,否則空間不會回收
如果刪除的是page頁,頁的復用沒有對id相同的要求
案例:
模擬數據 不斷插入刪除數據
CREATE TABLE `gaoshuiwei` ( `rid` int(20) NOT NULL AUTO_INCREMENT, `org_name` varchar(255) DEFAULT NULL, `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`rid`), UNIQUE KEY `org_id` (`rid`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=109592 DEFAULT CHARSET=utf8mb4;
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_gaoshuiwei`() BEGIN DECLARE i int(4) DEFAULT 0; while i < 10000 do insert into gaoshuiwei( org_name ) select '循環一般在存儲過程和存儲函數中使用' ; set i = i+1; end while; DELETE from gaoshuiwei where rid %2=1; END
查看表大小
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data
from information_schema.TABLES
where table_schema='test' and table_name='gaoshuiwei';
清理臟數據后-查看表大小
alter table table_name engine = InnoDB
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data
from information_schema.TABLES
where table_schema='test' and table_name='gaoshuiwei';