MySQL 5.7分区表使用普通列作为分区键时需要注意当前表中的主键、唯一键情况,如果两者都存在,那么就需要创建复合类型的主键和唯一键。
不过,一般都不这么做!这是MySQL分区表的局限! 创建复合唯一索引、主键复合索引——

CREATE TABLE sales (
    id VARCHAR(255) NOT NULL,
    product VARCHAR(50) NOT NULL,
    amount INT DEFAULT 1,
    created_at DATE NOT NULL,
    integer_part INT, -- 辅助列
    PRIMARY KEY (id, integer_part), -- 复合主键
    UNIQUE INDEX unique_product_integer_part (product, integer_part) -- 复合唯一索引
)
PARTITION BY RANGE (integer_part)
(
    PARTITION p0 VALUES LESS THAN (1000000),
    PARTITION p1 VALUES LESS THAN (2000000),
    PARTITION p2 VALUES LESS THAN (3000000),
    PARTITION p3 VALUES LESS THAN (4000000),
    PARTITION p4 VALUES LESS THAN (5000000),
    PARTITION p5 VALUES LESS THAN (6000000),
    PARTITION p6 VALUES LESS THAN (7000000),
    PARTITION p7 VALUES LESS THAN (8000000),
    PARTITION p8 VALUES LESS THAN (9000000),
    PARTITION p9 VALUES LESS THAN (10000000),
    PARTITION p10 VALUES LESS THAN (11000000),
    PARTITION p11 VALUES LESS THAN MAXVALUE
);