so I'm trying partitions for the first time in mysql any help will be much appreciated. What I want the partition to do is get before less than cur date for partition 1, the next week in partition 2 and max value for partition 3.
create TABLE diagnostics_daily_device_summary (
ID bigint(20) NOT NULL AUTO_INCREMENT,
timestamp datetime,
-- there are other fields included but for the sake of the example I won't bother throwing them in
PRIMARY KEY (ID, timestamp)
)
PARTITION BY RANGE (yearweek(timestamp))(
PARTITION p1 VALUES LESS THAN (yearweek(curdate())),
PARTITION p2 VALUES LESS THAN (yearweek(date_add(curdate(), interval 7 day))),
PARTITION p3 VALUES LESS THAN maxvalue
);
I need to maintain the last 3 weeks of data that's why I'm breaking it down into yearweek format.
When I run the query the error message I'm getting is this
Error Code: 1064. Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed near '), PARTITION p201547 VALUES LESS THAN (yearweek(date_add(curdate(), interval 7 d' at line 70
I'm not too sure how to fix it.
Additionally I want to add a new week every week and drop the last week, I got this line of code from an online source
USE demotimeparts;
DROP PROCEDURE IF EXISTS rotateTimePartition;
DELIMITER //
CREATE PROCEDURE rotateTimePartition (newPartValue DATETIME)
BEGIN
DECLARE keepStmt VARCHAR(2000) default @stms;
DECLARE partitionToDrop VARCHAR(64);
SELECT 201501
INTO partitionToDrop
-- FROM SCHEMA_NAME.partitions
WHERE table_schema='SCHEMA_NAME'
AND tabe_name='diagnostics_daily_device_summary'
AND partition_ordinary_position=1;
SET @stmt = CONCAT ('ALTER TABLE diagnostics_daily_device_summary DROP PARTITION' , partitionToDrop);
PREPARE pStmt FROM @stmt;
EXECUTE pStmt;
DEALLOCATE PREPARE pStmt;
SET @stmt = CONCAT ('ALTER TABLE diagnostics_daily_device_summary ADD PARTITION (PARTITION Y',
DATE_FORMAT(newPartValue, '%Y%m%d'),
' VALUES LESS THAN (TO_DAYS(\'',
DATE_FORMAT(newPartValue, '%Y-%m-%d'),
'\')))');
PREPARE pStmt FROM @stmt;
EXECUTE pStmt;
DEALLOCATE PREPARE pStmt;
SET @stmt = keepStmt;
END //
DELIMITER;
Like I said any help will be greatly appreciated! thank you again.