错误 1492 (HY000):对于 RANGE 分区,必须定义每个分区

ERROR 1492 (HY000): For RANGE partitions each partition must be defined

我正在尝试创建分区 table 但我收到错误消息

ERROR 1492 (HY000): For RANGE partitions each partition must be defined

我是不是漏掉了什么。查询如下所示:

CREATE TABLE IF NOT EXISTS logdata (
    `timestamp` datetime NOT NULL,
    `device_id` int(11) NOT NULL,
    `row_id` int(11) NOT NULL,
    `value_0` DECIMAL(27,9) DEFAULT NULL, 
    `value_1` DECIMAL(27,9) DEFAULT NULL, 
    `value_2` DECIMAL(27,9) DEFAULT NULL, 
    `value_3` DECIMAL(27,9) DEFAULT NULL, 
    `value_4` DECIMAL(27,9) DEFAULT NULL, 
    `value_5` DECIMAL(27,9) DEFAULT NULL, 
    `value_6` DECIMAL(27,9) DEFAULT NULL, 
    `value_7` DECIMAL(27,9) DEFAULT NULL, 
    `value_8` DECIMAL(27,9) DEFAULT NULL, 
    `value_9` DECIMAL(27,9) DEFAULT NULL,
    PRIMARY KEY (`timestamp`,`device_id`,`row_id`),
    KEY `device_id_1_202004` (`device_id`),
    KEY `row_id_1_202004` (`row_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED 
PARTITION BY RANGE(TO_DAYS(`timestamp`)) 
SUBPARTITION BY HASH (`device_id`) PARTITIONS 2 
(
    PARTITION p2001xx VALUES LESS THAN (TO_DAYS('2001-01-01 00:00:00')) (
        SUBPARTITION s2001xxa, 
        SUBPARTITION s2001xxb
    ),
    PARTITION p202004 VALUES LESS THAN (TO_DAYS('2020-04-01 00:00:00')) (
        SUBPARTITION s202004a, 
        SUBPARTITION s202004b
    ),
    PARTITION pMax VALUES LESS THAN MAXVALUE (
        SUBPARTITION smaxa, 
        SUBPARTITION smaxb
    )
);

如果指定子分区数,则使用SUBPARTITIONS 2,而不是PARTITIONS 2:

...
PARTITION BY RANGE(TO_DAYS(`timestamp`)) 
SUBPARTITION BY HASH (`device_id`) SUBPARTITIONS 2 
(
...

如果您明确命名子分区,那么您并不严格需要 SUBPARTITIONS 2:

...
PARTITION BY RANGE(TO_DAYS(`timestamp`)) 
SUBPARTITION BY HASH (`device_id`)
(
    PARTITION p2001xx VALUES LESS THAN (TO_DAYS('2001-01-01 00:00:00')) (
        SUBPARTITION s2001xxa, 
        SUBPARTITION s2001xxb
    ),
    PARTITION p202004 VALUES LESS THAN (TO_DAYS('2020-04-01 00:00:00')) (
        SUBPARTITION s202004a, 
        SUBPARTITION s202004b
    ),
    PARTITION pMax VALUES LESS THAN MAXVALUE (
        SUBPARTITION smaxa, 
        SUBPARTITION smaxb
    )
);