mysql 分区表中如何解决分区字段不是主键的情况? | mysql | mysql 技术论坛-380玩彩网官网入口

    请教各位大佬一个问题,mysql 在创建分区表的时候,似乎要求分区字段必须是主键或者是主键的一部分。
    目前我在业务上遇到一个问题,有一张大的数据分层表,累加了很多数据,现在把最多的一级数据分离出一张新表,我想根据这一级数据中的时间类型(2024-01-01) 来设置分区,但是又要兼顾原有的业务逻辑,所以想保留原有主键(id) 的同时,建立分区,代码内容如下,求助各位大佬看看有没有什么好的380玩彩网官网入口的解决方案。
create table orders (
    order_id int not null auto_increment,
    order_date date not null,
    customer_id int,
    amount decimal(10, 2),
    primary key (order_id),  -- 设置order_id为主键 
)
partition by range (year(order_date))  -- 根据order_date的年份进行分区
(
    partition p2021 values less than (2022),
    partition p2022 values less than (2023),
    partition p2023 values less than (2024),
    partition p_future values less than maxvalue
);
    这样建会提示错误信息如下:
    sql 错误 [1503] [hy000]: a primary key must include all columns in the table's partitioning function (prefixed columns are not considered).
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图