I want to create a log table in 5.1 that is partitioned by day. Â I want to roll-off old data and create new partitions for each day automatically. Â Without writing a script and a cronjob, it seems like this should be possible with Events. Â Let's start with the table:
create table log ( logged datetime not null, id int not null auto_increment, text varchar(256), PRIMARY KEY ( logged, id ) ) PARTITION BY RANGE( TO_DAYS( logged ) ) ( PARTITION p20080206 VALUES LESS THAN (733444), PARTITION p20080207 VALUES LESS THAN (733445), PARTITION p20080208 VALUES LESS THAN (733446) );
This seems pretty straight-forward: Â I take my log entry time and convert it TO_DAYS and partition on that. Â I have tomorrow's partition all ready to go, just in case I don't get around to adding it today. Â Let's create an Event to do add tomorrow's partition for us automatically each day: