修改 AUTO_INCRMENT PRIMARY KEY 进行分区

发布于 2024-11-08 15:28:39 字数 1129 浏览 7 评论 0原文

我需要在临时数据之间对 MySQL 表进行分区(字段在下表中开始)。

CREATE TABLE `table1` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT, 
  `fk_id` bigint(20) NOT NULL,  
  `begin` bigint(20) NOT NULL,     
  PRIMARY KEY (`id`),
  KEY `FK1E57078DB20EC268` (`fk_id`)
) ENGINE=MyISAM AUTO_INCREMENT=10443288 DEFAULT CHARSET=latin1

当我尝试像这样分区时:

alter table table1 partition by range (begin) (
PARTITION until_2010_07 VALUES LESS THAN (1280620800000),
PARTITION 2010_08 VALUES LESS THAN (1283299200000),
PARTITION 2010_09 VALUES LESS THAN (1285891200000),
PARTITION 2010_10 VALUES LESS THAN (1288569600000),
PARTITION 2010_11 VALUES LESS THAN (1291161600000),
PARTITION 2010_12 VALUES LESS THAN (1293840000000),
PARTITION from_2011 VALUES LESS THAN MAXVALUE
);

我收到 MySQL 错误:ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's Partitioning function

据我从 mysql 文档中了解到,分区列应该属于主键。 对我来说问题是我想将 PRIMARY_KEY 更改为复合键,即 PRIMARY KEY ('id','fk_id','begin') 而不更改现有的 id 列(因为它是一个应用程序中用于生成可添加书签的 url 的字段,因此无法对 ids 进行重新编号)

如何更改 PRIMARY_KEY 以便可以进行分区?

I need to partition a MySQL table amongst temporal data (field begin in the following table).

CREATE TABLE `table1` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT, 
  `fk_id` bigint(20) NOT NULL,  
  `begin` bigint(20) NOT NULL,     
  PRIMARY KEY (`id`),
  KEY `FK1E57078DB20EC268` (`fk_id`)
) ENGINE=MyISAM AUTO_INCREMENT=10443288 DEFAULT CHARSET=latin1

When I try to partition like this :

alter table table1 partition by range (begin) (
PARTITION until_2010_07 VALUES LESS THAN (1280620800000),
PARTITION 2010_08 VALUES LESS THAN (1283299200000),
PARTITION 2010_09 VALUES LESS THAN (1285891200000),
PARTITION 2010_10 VALUES LESS THAN (1288569600000),
PARTITION 2010_11 VALUES LESS THAN (1291161600000),
PARTITION 2010_12 VALUES LESS THAN (1293840000000),
PARTITION from_2011 VALUES LESS THAN MAXVALUE
);

I get a MySQL error : ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function

As I understand from the mysql doc, a partitioning column should belongs to the primary key.
The problem for me is that I want to change the PRIMARY_KEY to be a composite one ie PRIMARY KEY ('id','fk_id','begin') without changing the existing id columns (because it's a field that is used in the application to generate bookmarkable urls, so renumbering the ids is not an option)

How can I alter the PRIMARY_KEY so that I can do my partitioning ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2024-11-15 15:28:39

我终于找到了一种方法来做到这一点,我是为了其他遇到这个问题的人而回答

//drop auto_increment capability
alter table table1 change column id id BIGINT NOT NULL;
//in one line, drop primary key and rebuild one
alter table table1 drop primary key, add primary key(id,fk_id,begin);
//re add the auto_increment capability, last value is remembered
alter table table1 change column id id BIGINT NOT NULL AUTO_INCREMENT;
//build the partition
alter table table1 partition by range (begin) ( 
    PARTITION until_2010_07 VALUES LESS THAN (1280620800000), 
    PARTITION 2010_08 VALUES LESS THAN (1283299200000), 
    PARTITION 2010_09 VALUES LESS THAN (1285891200000), 
    PARTITION 2010_10 VALUES LESS THAN (1288569600000), 
    PARTITION 2010_11 VALUES LESS THAN (1291161600000), 
    PARTITION 2010_12 VALUES LESS THAN (1293840000000), 
    PARTITION from_2011 VALUES LESS THAN MAXVALUE 
);

分区确实非常简单,但我建议人们在创建主键时考虑一下,然后再在生产中实际使用数据库:- )

在我的笔记本电脑上,每个步骤需要 3 分钟,然后我必须停止服务以保持数据库的一致性

I finally found a way to do it, I answer for the sake of other people who come across this question

//drop auto_increment capability
alter table table1 change column id id BIGINT NOT NULL;
//in one line, drop primary key and rebuild one
alter table table1 drop primary key, add primary key(id,fk_id,begin);
//re add the auto_increment capability, last value is remembered
alter table table1 change column id id BIGINT NOT NULL AUTO_INCREMENT;
//build the partition
alter table table1 partition by range (begin) ( 
    PARTITION until_2010_07 VALUES LESS THAN (1280620800000), 
    PARTITION 2010_08 VALUES LESS THAN (1283299200000), 
    PARTITION 2010_09 VALUES LESS THAN (1285891200000), 
    PARTITION 2010_10 VALUES LESS THAN (1288569600000), 
    PARTITION 2010_11 VALUES LESS THAN (1291161600000), 
    PARTITION 2010_12 VALUES LESS THAN (1293840000000), 
    PARTITION from_2011 VALUES LESS THAN MAXVALUE 
);

Partitioning is indeed really easy, but I advice people to think about it while creating the primary key before actually using their database in production :-)

Each step takes 3 minutes on my laptop, then i'll have to stop the service to keep consistency in my database

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文