Mysql 分区表删除

2021-01-08  本文已影响0人  HueyYao

分区表删除部分分区

使用场景:从 MySQL 5.1 开始,支持分区

创建日志表时建议使用分区方式

use test;

drop table if exists t29_log ;

CREATE TABLE t29_log (
    id INT,
    log_info VARCHAR (100),
    date datetime
) ENGINE = INNODB PARTITION BY RANGE (YEAR(date))(
    PARTITION p2016
    VALUES
        less THAN (2017),
        PARTITION p2017
    VALUES
        less THAN (2018),
        PARTITION p2018
    VALUES
        less THAN (2019)
);

在上表的分区表中,表示:

插入数据测试

insert into t29_log select 1,'aaa','2016-01-01';
insert into t29_log select 2,'bbb','2016-06-01';
insert into t29_log select 3,'ccc','2017-01-01';
insert into t29_log select 4,'ddd','2018-01-01';

通过sql来查询过分区信息

select TABLE_SCHEMA,TABLE_NAME,PARTITION_NAME,TABLE_ROWS from information_schema.partitions where table_schema='test' and table_name='t29_log';

通过

select * from t29_log;

查询数据库信息后

执行

alter table t29_log drop partition p2016;

在检索数据就可以看到 所属于p2016得数据已经被删除了

因此,对于要经常删除历史数据的表,建议配置成分区表。以方便后续历史数据删除。

上一篇 下一篇

猜你喜欢

热点阅读