MySQL分区表操作

 

-- 查看分区表信息

SHOW CREATE TABLE table1;

SHOW TABLE STATUS LIKE 'table1';



-- 查看各分区信息

SELECT TABLE_NAME,PARTITION_NAME,PARTITION_EXPRESSION,PARTITION_DESCRIPTION,TABLE_ROWS,AVG_ROW_LENGTH/1024 as 'AVG_ROW_LENGTH (KB)',DATA_LENGTH/(1024*1024) as 'DATA_LENGTH (MB)',MAX_DATA_LENGTH/(1024*1024*1024*1024) as 'MAX_DATA_LENGTH (TB)',INDEX_LENGTH/(1024*1024) as 'INDEX_LENGTH (MB)',UPDATE_TIME,CHECK_TIME

FROM INFORMATION_SCHEMA.PARTITIONS

WHERE TABLE_NAME='table1' AND TABLE_SCHEMA='db1';



-- 生成truncate/drop 3个月前分区的SQL  以日期分区类型, 其它的如按周分区的, 修改对应的条件即可

SELECT concat('alter table ',TABLE_NAME,' truncate partition ',PARTITION_NAME,';') as '执行删除分区的SQL', TABLE_NAME,PARTITION_NAME,PARTITION_EXPRESSION,PARTITION_DESCRIPTION,TABLE_ROWS,AVG_ROW_LENGTH/1024 as 'AVG_ROW_LENGTH (KB)',DATA_LENGTH/(1024*1024) as 'DATA_LENGTH (MB)',MAX_DATA_LENGTH/(1024*1024*1024*1024) as 'MAX_DATA_LENGTH (TB)',INDEX_LENGTH/(1024*1024) as 'INDEX_LENGTH (MB)',UPDATE_TIME,CHECK_TIME

FROM INFORMATION_SCHEMA.PARTITIONS

WHERE TABLE_NAME='table1' AND TABLE_SCHEMA='db1' 

and STR_TO_DATE(SUBSTR(PARTITION_DESCRIPTION FROM 2 FOR 10),'%Y-%m-%d') BETWEEN '2011-10-01' and DATE_SUB(CURRENT_DATE,INTERVAL 3 MONTH);

你可能感兴趣的:(mysql)