我需要每天根据UTC时间戳对我的表进行分区,我收到的错误为Fatal error: A PRIMARY KEY must include all columns in the table's partitioning function
我这样做,列id
是INT PRIMARY KEY列date
是INT存储UTC时间戳
我的表格结构DESCRIBE stats;
Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
date int(11) YES NULL
.
.
.
.
创建表格结构SHOW CREATE TABLE stats
CREATE TABLE `stats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` int(11) DEFAULT NULL,
.
.
.
.
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=371 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
我通过php执行脚本
$today = strtotime("today midnight");
$partition = date('d_m_y',$today);
ALTER TABLE `stats`
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today)
)
我试过这个但是语法错误
ALTER TABLE `stats`
UNIQUE KEY (`id`)
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today)
)
ALTER TABLE `stats`
PRIMARY KEY (`id`)
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today)
)
ALTER TABLE `stats`
PRIMARY KEY (`id`,`date`)
PARTITION BY RANGE (date) (
PARTITION p_$partition VALUES LESS THAN ($today)
)
还尝试使用HASH
但同样的错误A PRIMARY KEY must include all columns in the table's partitioning function in
ALTER TABLE stats
PARTITION BY HASH(date)
PARTITIONS 1
也尝试过这种方式,但收到错误Fatal error: Partition management on a not partitioned table is not possible
ALTER TABLE `stats`
ADD PARTITION (
PARTITION p_$partition VALUES LESS THAN ($today)
)
请参阅并建议任何可行的方法。
由于
答案 0 :(得分:5)
我通过将date
添加到主键id
来解决了这个问题。
ALTER TABLE `stats` DROP PRIMARY KEY, ADD PRIMARY KEY(`id`,`date`);
现在表已成功分区。
希望它也有助于其他人。
由于