我有很多表,由于某些原因,我需要在应用程序启动时调整这些表的自动增量值。
我尝试这样做:
mysql> select max(id) from item;
+----------+
| max(id) |
+----------+
| 97972232 |
+----------+
1 row in set (0.05 sec)
mysql> alter table item auto_increment=1097972232;
在另一个会话中:
afrolov@A1-DB1:~$ mysql -u root -e "show processlist" | grep auto_increment
472196 root localhost test Query 39 copy to tmp table alter table item auto_increment=1097972232
MySQL 开始重建表了!为什么MySQL需要这么做呢?如何在调整 auto_increment 值时避免重建巨大的表?
MySQL 5.0、InnoDB。
表定义:
CREATE TABLE `item` (
`id` bigint(20) NOT NULL auto_increment,
`item_res_id` int(11) NOT NULL default '0',
`stack_count` int(11) NOT NULL default '0',
`position` int(11) NOT NULL default '0',
`place` varchar(15) NOT NULL default '',
`counter` int(11) NOT NULL default '-1',
`is_bound` tinyint(4) NOT NULL default '0',
`remove_time` bigint(20) NOT NULL default '-1',
`rune_res_id` int(11) default NULL,
`rune_id` bigint(20) default NULL,
`avatar_id` bigint(20) NOT NULL,
`rune_slot_res_id` int(11) default NULL,
`is_cursed` tinyint(4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `avatar_id` (`avatar_id`,`place`,`position`),
UNIQUE KEY `rune_id` (`rune_id`),
KEY `idx_item_res_id` (`item_res_id`)
) ENGINE=InnoDB AUTO_INCREMENT=97972233 DEFAULT CHARSET=utf8;
关于为什么我必须这样做。长话短说,我想解决有关在服务器重新启动时重置 auto_increment 值的 mysql innodb 问题。有时我们将表中的行复制到另一个表,并且必须保持行 ID 不变。当我们向 table1 添加一行(例如 id=1),将行复制到 table2 ,从 table1 中删除行并重新启动 MySQL 时,当我们在 table1 中创建新的一行时,该行也将获得 id=1 。因此,如果我们必须将行复制到 table2,我们就会违反唯一约束。我们已经有很多代码,很难全部重写。调整自动增量值似乎是解决此问题的最简单方法。
Added:
MySQL 5.5 - 都一样:(