InnoDB的"auto_increment"指定值被重置问题
有时候新建的表需要对自增列指定初始值,但是有时候会出现明明指定过的初始值却被重置的现象。下面以一个小实验来说明这个问题:
MySQL version:5.1.42 OS:redhat5.3
无废话,建张表先:
代码
CREATE TABLE `sbtest1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=20000000 DEFAULT CHARSET=latin1;
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=20000000 DEFAULT CHARSET=latin1;
诸位看官可以看见我指定了auto_increment=20000000。
表建好后,如果紧接着插数据,那么此现象是不会出现的:
代码
mysql> insert into sbtest1(k,pad) values(123,'what are you doing');
Query OK, 1 row affected (0.00 sec)
mysql> select * from sbtest1;
+----------+-----+---+--------------------+
| id | k | c | pad |
+----------+-----+---+--------------------+
| 20000000 | 123 | | what are you doing |
+----------+-----+---+--------------------+
1 row in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> select * from sbtest1;
+----------+-----+---+--------------------+
| id | k | c | pad |
+----------+-----+---+--------------------+
| 20000000 | 123 | | what are you doing |
+----------+-----+---+--------------------+
1 row in set (0.00 sec)
那什么时候?嗯,确切地说是做什么样的操作,重置现象才会出现呢?看实验:
代码
CREATE TABLE `sbtest1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=20000000 DEFAULT CHARSET=latin1
mysql> quit
Bye
[root@test_2 ~]# service mysqld restart
mysql> show create table sbtest1 \G
*************************** 1. row ***************************
Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k` (`k`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=20000000 DEFAULT CHARSET=latin1
mysql> quit
Bye
[root@test_2 ~]# service mysqld restart
mysql> show create table sbtest1 \G
*************************** 1. row ***************************
Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`k` int(10) unsigned NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k` (`k`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
这时,auto_increment的值被重置了!
通过以上的小实验,结合手册就能理解为什么会发生这种现象了:
如果对某张innodb表指定了auto_increment,那么innodb就会在data dictionary为它维护一个auto_increment计数器,注意:这个计数器只存储在内存中,不会写在disk上。
那么 innodb在DB重启后,如何对这个计数器进行初始化呢(结合实验中的这张表来说明)?
InnoDB在数据库重新启动后,它会对指定过 auto_increment的表(sbtest1)做这样一个操作:
SELECT MAX(id) FROM sbtest1 FOR UPDATE;
然后将这条语句取得的值+1赋给被指定auto_increment的字段和在内存中的这张表的计数器。如果表是空的,那么这个值将会是1.
解决方法:
1.重建完表后,插入数据之前不要重启(说白了,就是要保证内存的数据不会释放)
2.插入一条脏数据