Omkar Mozar
Omkar Mozar

Reputation: 159

Automation of mysql partitioning

I have searched lot about automation of mysql partitioning. But unfortunately nothing matches to problem. I want delete an older partitions which are not needed but at the same time add new upcoming data to partition. What I can do here is every day drop an older partition and create new partitions with some automated functions of mysql such as MONTH(NOW()-interval 2 month) etc. But what it will do is increase the cost of operation as every night I need recreate the partitions for new data. I found that i can use partitioning by range but there are all hardcoded examples suggest's that i might need to do partitioning every time new data gets added.

Here is an example I found but not much similar to me :

ALTER TABLE t1 PARTITION BY 
RANGE(TO_DAYS(FROM_UNIXTIME(transaction_date)))(
PARTITION JAN VALUES LESS THAN (TO_DAYS('2013-02-01')),
PARTITION FEB VALUES LESS THAN (TO_DAYS('2013-03-01')),
PARTITION MAR VALUES LESS THAN (TO_DAYS('2013-04-01')),
PARTITION APR VALUES LESS THAN (TO_DAYS('2013-05-01')),
PARTITION MAY VALUES LESS THAN (TO_DAYS('2013-06-01')),
PARTITION JUN VALUES LESS THAN (TO_DAYS('2013-07-01')),
PARTITION JUL VALUES LESS THAN (TO_DAYS('2013-08-01')),
PARTITION AUG VALUES LESS THAN (TO_DAYS('2013-09-01')),
PARTITION SEP VALUES LESS THAN (TO_DAYS('2013-10-01')),
PARTITION `OCT` VALUES LESS THAN (TO_DAYS('2013-11-01')),
PARTITION NOV VALUES LESS THAN (TO_DAYS('2013-12-01')),
PARTITION `DEC` VALUES LESS THAN (TO_DAYS('2014-01-01'))
);

Please suggest me a proper way to do it.

Upvotes: 1

Views: 229

Answers (2)

bharatharatna puli
bharatharatna puli

Reputation: 63

You can do it like this. This will automatically store the data in the corresponding partitions. Regarding the automation of truncating them, I too am exploring the creation of scheduled events.

PARTITION BY RANGE ( month(creationDate))
(PARTITION p0 VALUES LESS THAN (2) ENGINE = InnoDB,
 PARTITION p1 VALUES LESS THAN (3) ENGINE = InnoDB,
 PARTITION p2 VALUES LESS THAN (4) ENGINE = InnoDB,
 PARTITION p3 VALUES LESS THAN (5) ENGINE = InnoDB,
 PARTITION p4 VALUES LESS THAN (6) ENGINE = InnoDB,
 PARTITION p5 VALUES LESS THAN (7) ENGINE = InnoDB,
 PARTITION p6 VALUES LESS THAN (8) ENGINE = InnoDB,
 PARTITION p7 VALUES LESS THAN (9) ENGINE = InnoDB,
 PARTITION p8 VALUES LESS THAN (10) ENGINE = InnoDB,
 PARTITION p9 VALUES LESS THAN (11) ENGINE = InnoDB,
 PARTITION p10 VALUES LESS THAN (12) ENGINE = InnoDB,
 PARTITION p11 VALUES LESS THAN (13) ENGINE = InnoDB,
 PARTITION p12 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */ |

This can be extended to creating partitions based on a week as well.

Upvotes: 0

Rick James
Rick James

Reputation: 142208

There is no fully automated way -- You need to write code.

But first, let's fix an issue. Have another partition:

PARTITION future VALUES LESS THAN (MAXVALUE)

This will come in handy if you accidentally fail to roll the partitions some night.

And how about a bug: Your table essentially never has a full 12 months of data. Just after a sliding of the partitions, you will have only 11 months. Is that OK? If not, keep 13 months, not 12.

Now for some code to do the work, plus perhaps some more tips: http://mysql.rjweb.org/doc.php/partitionmaint

Upvotes: 1

Related Questions