MariaDB Partition By Range - Month with DATE, DATETIME, and TIMESTAMP using TO_DAYS

132 Views Asked by At

Will this usage of partitioning by range using days to get a monthly partition using TO_DAYS work ?:

CREATE TABLE IF NOT EXISTS `T_Active_Alarm` (
    `AA_ID` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `AlarmRA_ID` int unsigned NOT NULL,
    `AA_Begin_Hour` datetime NOT NULL,
    `AA_End_Hour` datetime DEFAULT NULL,
    `AA_Delayed` tinyint unsigned NOT NULL DEFAULT 0
    CONSTRAINT 'idx_aa_aa_id_begin_hour_end_hour_delayed'
    INDEX (`AA_ID`,`AA_Begin_Hour`,`AA_End_Hour`,`AA_Delayed`),
    CONSTRAINT `fk_aa_AlarmRA_id` 
    FOREIGN KEY (`AlarmRA_ID`) REFERENCES `T_AlarmRA` (`AlarmRA_ID`) ON UPDATE RESTRICT ON DELETE RESTRICT
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=
    PARTITION BY RANGE (TO_DAYS(AA_End_Hour)) (
    PARTITION AA_2023_10 VALUES LESS THAN (TO_DAYS('2023-11-01 00:00:00')),
    PARTITION AA_2023_11 VALUES LESS THAN (TO_DAYS('2023-12-01 00:00:00')),
    PARTITION AA_2023_12 VALUES LESS THAN (TO_DAYS('2024-01-01 00:00:00')),
    PARTITION AA_2024_01 VALUES LESS THAN (TO_DAYS('2024-02-01 00:00:00')),
    PARTITION Future VALUES LESS THAN (MAXVALUE)
     );

Can this be done ? and can it be done like this? Or is there a better way for datetime ?

I can't use the word TO_MONTHS, can I ? I've never seen that anywhere ...

And if my datatype is DATE (instead of datetype) do I need to write the sql like this:

PARTITION AA_2024_01 VALUES LESS THAN (TO_DAYS('2024-02-01')),

Or doen't the extra 00:00:00 matter ?

And lastly what if it's a DATETIME(4) ? Do I have to add 00:00:00.0000 ?

These are serious questions, I can't seem to find these answers anywhere. Thanks for helping me to understand Partition by Range usage.

Of course I'm going to create a job to prune the partitions every 1st of the month at 00:00:00 and then again (if not already done) at 01:00:00, and again at 07:00:00 if not already done, or if not done within the last 12 hours or within the current day.

0

There are 0 best solutions below