简体   繁体   English

MySQL的自动增量增加2和1?

[英]Mysql Auto Increment increasing by 2 and 1?

If I create a table with the following syntax, 如果我使用以下语法创建表,

CREATE TABLE IF NOT EXISTS `hashes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`hash` binary(20) NOT NULL,
PRIMARY KEY (`id`,`hash`),
UNIQUE KEY (`hash`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE = 4 AUTO_INCREMENT=1
PARTITION BY KEY(`hash`)
PARTITIONS 10;

And insert queries with the following syntax 并使用以下语法插入查询

INSERT INTO hashes (hash) VALUES ($value) ON DUPLICATE KEY UPDATE hash = hash

Then the auto increment column works as expected both if the row is inserted or updated. 然后,如果插入或更新了行,则自动增量列将按预期工作。

Although creating the table without the partition like below and inserting with the query above the auto increment value will increase by 1 on every update or insert causing the A_I column to be all over place as the query could do 10 updates and then 1 insert causing the column value to jump 10 places. 尽管创建不具有如下所示分区的表并在查询上方插入时,自动增量值将在每次更新或插入时增加1,从而导致A_I列变满,因为查询可以进行10次更新,然后执行1次插入,导致列值跳10位。

CREATE TABLE IF NOT EXISTS `hashes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`hash` binary(20) NOT NULL,
PRIMARY KEY (`id`,`hash`),
UNIQUE KEY (`hash`)
) ENGINE=InnoDB AUTO_INCREMENT=1;

I understand why the value increases on an update with INNO_DB but I do not understand why it doesn't when the table is partitioned? 我知道为什么在使用INNO_DB更新时值会增加,但我不明白为什么在对表进行分区时不会增加值?

you cannot change that, but you can try something like this: 您无法更改,但是可以尝试如下操作:

mysql> set @a:= (select max(id) + 2 from hashes); 

mysql> insert into hashes (id) values ($value) on duplicate key update id=@a;

NOTE: the partitions change a little bit after mysql 5.6, which version do you have? 注意:在mysql 5.6之后,分区会有一点变化,您拥有哪个版本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM