mysql - 为什么auto_increment id不逐一递增,怎么设置?

标签 mysql primary-key mariadb

我有一个 MariaDB Galera 集群(3 个节点),我将 uid 设置为自动增加并作为表的主键

 `uid       | int(11)     | NO   | PRI | NULL    | auto_increment`. 


 MariaDB [hello_cluster]> select uid from table order by uid limit 10;
    +-----+
    | uid |
    +-----+
    |   3 |
    |   6 |
    |   9 |
    |  12 |
    |  15 |
    |  18 |
    |  21 |
    |  24 |
    |  27 |
    |  30 |
    +-----+

我试过下面的命令,还是不行

 alter table uid AUTO_INCREMENT=1

最佳答案

这是设计使然,并在 MariaDB Galera Cluster - Known Limitations 中报告:

Do not rely on auto-increment values to be sequential. Galera uses a mechanism based on autoincrement increment to produce unique non-conflicting sequences, so on every single node the sequence will have gaps.

理性在 Managing Auto Increments with Multi Masters 中解释。 , 这也是为什么观察到的自动增量与聚类数量步长相同的原因。

MySQL has system variables auto_increment_increment and auto_increment_offset for managing auto increment 'sequences' in multi master environment. Using these variables, it is possible to set up a multi master replication, where auto increment sequences in each master node interleave, and no conflicts should happen in the cluster. No matter which master(s) get the INSERTs.

即使没有集群,由于事务回滚和删除记录,依赖自增列成为密集序列也很少是一个“好”主意。

关于mysql - 为什么auto_increment id不逐一递增,怎么设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618934/

相关文章:

gradle - JHipster 6.0.1 : Liquibase tasks fail

grails - grails 应用程序中的 Mysql 连接超时,在 ELB 上使用 mariadb galera 集群

javascript - 我可以通过从 PHP 获取变量来更改 Javascript 中的显示属性吗?

sql - 用一个字符长的单词搜索(MySQL)

mysql - 如何在行版本化MySQL表(SCD类型2)中设置主键、自增和索引?

MySQL使用分区并保持主键不变

mysql - SELECT JOIN WHERE 记录不存在

MySQL在一列中选择具有不同值的相同ID的不同行并排除另一列中的特定值

sql-server - 当你的主键溢出时你会怎么做?

mysql - 如何将mysql日志重定向到容器内的/dev/stdout?