修改 AUTO_INCRMENT PRIMARY KEY 进行分区
我需要在临时数据之间对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于找到了一种方法来做到这一点,我是为了其他遇到这个问题的人而回答
分区确实非常简单,但我建议人们在创建主键时考虑一下,然后再在生产中实际使用数据库:- )
在我的笔记本电脑上,每个步骤需要 3 分钟,然后我必须停止服务以保持数据库的一致性
I finally found a way to do it, I answer for the sake of other people who come across this question
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