- Details
- Written by: po3dno
- Category: MariaDB
- Hits: 800
ALTER TABLE e_5136_copy
REORGANIZE PARTITION p_future INTO (
PARTITION p_202201 VALUES LESS THAN (TO_DAYS('2022-02-01 00:00:00')),
PARTITION p_future VALUES LESS THAN MAXVALUE);
ALTER TABLE e_5136
REORGANIZE PARTITION p_future INTO (
PARTITION p_202201 VALUES LESS THAN (TO_DAYS('2022-02-01 00:00:00')),
PARTITION p_202202 VALUES LESS THAN (TO_DAYS('2022-03-01 00:00:00')),
PARTITION p_202203 VALUES LESS THAN (TO_DAYS('2022-04-01 00:00:00')),
PARTITION p_202204 VALUES LESS THAN (TO_DAYS('2022-05-01 00:00:00')),
PARTITION p_202205 VALUES LESS THAN (TO_DAYS('2022-06-01 00:00:00')),
PARTITION p_202206 VALUES LESS THAN (TO_DAYS('2022-07-01 00:00:00')),
PARTITION p_202207 VALUES LESS THAN (TO_DAYS('2022-08-01 00:00:00')),
PARTITION p_202208 VALUES LESS THAN (TO_DAYS('2022-09-01 00:00:00')),
PARTITION p_202209 VALUES LESS THAN (TO_DAYS('2022-10-01 00:00:00')),
PARTITION p_202210 VALUES LESS THAN (TO_DAYS('2022-11-01 00:00:00')),
PARTITION p_202211 VALUES LESS THAN (TO_DAYS('2022-12-01 00:00:00')),
PARTITION p_202212 VALUES LESS THAN (TO_DAYS('2023-01-01 00:00:00')),
PARTITION p_future VALUES LESS THAN MAXVALUE);
- Details
- Written by: po3dno
- Category: MariaDB
- Hits: 933
alter table app_log_Test drop PRIMARY KEY, add primary key (`id`, `dateCreated`);
Next, I can re-run my alter table to add the partitions I care about.
ALTER TABLE app_log_Test
PARTITION BY RANGE (TO_DAYS(dateCreated))
(PARTITION p_invalid_date VALUES LESS THAN (0) ENGINE = TokuDB,
PARTITION p_201809 VALUES LESS THAN (TO_DAYS('2018-09-01 00:00:00')),
PARTITION p_201810 VALUES LESS THAN (TO_DAYS('2018-10-01 00:00:00')),
PARTITION p_max_future_dates VALUES LESS THAN MAXVALUE);
If I need to add more partitions after that. I don't have to specify the partition scheme again I can just add the partition and its constraints.
ALTER TABLE app_log_Test
REORGANIZE PARTITION p_max_future_dates INTO (
PARTITION p_201811 VALUES LESS THAN (TO_DAYS('2018-11-01 00:00:00')),
PARTITION p_201812 VALUES LESS THAN (TO_DAYS('2018-12-01 00:00:00')),
PARTITION p_max_future_dates VALUES LESS THAN MAXVALUE);
My table now looks like this.
show create table app_log_Test;
Create Table: CREATE TABLE `app_log_Test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`dateCreated` datetime NOT NULL,
`host` varchar(512) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`label` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`event` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`level` varchar(8) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`message` text COLLATE utf8mb4_unicode_ci,
`version` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`,`dateCreated`),
KEY `app_log_dateCreated` (`dateCreated`),
KEY `app_log_label` (`label`),
KEY `app_log_event` (`event`),
KEY `app_log_level` (`level`)
) ENGINE=TokuDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci `COMPRESSION`=tokudb_zlib
- Details
- Written by: po3dno
- Category: MariaDB
- Hits: 950
A MariaDB Support customer recently asked how they could automatically drop old partitions after 6 months. MariaDB does not have a mechanism to do this automatically out-of-the-box, but it is not too difficult to create a custom stored procedure and an event to call the procedure on the desired schedule. In fact, it is also possible to go even further and create a stored procedure that can also automatically add new partitions. In this blog post, I will show how to write stored procedures that perform these tasks.
PARTITIONED TABLE DEFINITION
For this demonstration, I’ll use a table definition based on one from MySQL’s documentation on range partitioning, with some minor changes:
DROP TABLE IF EXISTS db1.quarterly_report_status; CREATE TABLE db1.quarterly_report_status ( report_id INT NOT NULL, report_status VARCHAR(20) NOT NULL, report_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB PARTITION BY RANGE ( UNIX_TIMESTAMP(report_updated) ) ( PARTITION p_first VALUES LESS THAN ( UNIX_TIMESTAMP('2016-10-01 00:00:00')), PARTITION p201610 VALUES LESS THAN ( UNIX_TIMESTAMP('2016-11-01 00:00:00')), PARTITION p201611 VALUES LESS THAN ( UNIX_TIMESTAMP('2016-12-01 00:00:00')), PARTITION p201612 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-01-01 00:00:00')), PARTITION p201701 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-02-01 00:00:00')), PARTITION p201702 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-03-01 00:00:00')), PARTITION p201703 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-04-01 00:00:00')), PARTITION p201704 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-05-01 00:00:00')), PARTITION p201705 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-06-01 00:00:00')), PARTITION p201706 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-07-01 00:00:00')), PARTITION p201707 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-08-01 00:00:00')), PARTITION p201708 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-09-01 00:00:00')), PARTITION p_future VALUES LESS THAN (MAXVALUE) );