今天排查一个问题,迁移工具(公司内部同步工具),迁移的数据少一条,好奇怪,由于迁移表依据primary key切分任务
大概率会是收尾处数据丢失。
假设有表tb1,其定义如下:
CREATE TABLE tb1
(
id
int(11) NOT NULL AUTO_INCREMENT,
c1
int(11) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
源:
mysql> select min(id),max(id) from tbl;
+---------+---------+
| min(id) | max(id) |
+---------+---------+
| 0 | 157087 |
+---------+---------+
1 row in set
目标:
mysql> select min(id),max(id) from t_hash;
+---------+---------+
| min(id) | max(id) |
+---------+---------+
| 1 | 157083 |
+---------+---------+
1 row in set
通过添加日志,调试迁移工具;
- 任务切分没有问题,包含id为0行记录;
- pull数据的操作没有问题,拉取数据包含id为0行记录;
- push数据的操作,也没有问题,操作中也是包含id为0行的记录;
突然想起来前几天,关于自增列的问题,如果insert的自增列值为0,会变成1;
验证:
肯定是没问题的:
insert into tb1 VALUES(1,1);
insert into tb1(id,c1)VALUES(2,2);
负值的自增值也没有问题:
INSERT INTO tb1(id,c1)VALUES(-1,-1);
那插入0呢?
INSERT INTO tb1(id,c1)VALUES(0,0);
虽然显示插入自增值为0,但是自增值变成了1,这是啥意思呢?
查看了下MySQL帮助文档,发现该问题和一个变量有关:NO_AUTO_VALUE_ON_ZERO,一般情况下,在插入记录时,如果自增列对于的数据为NULL或0时,系统会获取一个新的自增值来作为本次插入的自增值使用,如果希望将0作为自增值来使用,那些需要设置SQL MODE 为NO_AUTO_VALUE_ON_ZERO,如:
SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';
官方文档如下:
NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.
This mode can be useful if 0 has been stored in a table's AUTO_INCREMENT column. (Storing 0 is not a recommended practice, by the way.) For example, if you dump the table with mysqldump and then reload it, MySQL normally generates new sequence numbers when
it encounters the 0 values, resulting in a table with contents different from the one that was dumped. Enabling NO_AUTO_VALUE_ON_ZERO before reloading the dump file solves this problem. mysqldump now automatically includes in its output a statement that enables NO_AUTO_VALUE_ON_ZERO, to avoid this problem.
=======================================================================
尽管可以通过NO_AUTO_VALUE_ON_ZERO来将自增值设置为0,但不建议将自增值设置为0,以避免在数据迁移或同步时因环境设置不同而导致数据差异,如从库未设置NO_AUTO_VALUE_ON_ZERO的情况下通过mysqldump导入初始化数据,便会导致数据不一直或初始化失败。
=======================================================================
网友评论