Home  >  Article  >  Database  >  MySQL auto_increment实现

MySQL auto_increment实现

WBOY
WBOYOriginal
2016-05-31 08:47:181093browse

运维的时候,经常遇到auto_increment的疑惑:

  1. 机器异常crash,重启后id回退的问题
  2. 性能考虑,每次获取肯定不会持久化,内存中取值,statement复制如何保证主备一致
  3. id的取值受binlog的保护吗

1. auto_increment相关的参数控制

1.1 innodb_autoinc_lock_mode

0: 每一个statement获取一个排他lock,直到statement结束,保证statement执行过程的id是连续的。

1: 单条确定insert影响的条数的时候,使用mutex。如果是insert select,load data这样的,使用排他lock。

2: 多条statement产生的id会穿插在一起,如果是statement复制,会产生不一致的情况。

1.2

auto_increment_increment

auto_increment_offset

控制自增的起始值和interval

2. auto_increment相关的数据结构

1. 锁模式中LOCK_AUTO_INC,即auto_increment的表锁。

/* Basic lock modes */enum lock_mode {	LOCK_IS = 0,	/* intention shared */	LOCK_IX,	/* intention exclusive */	LOCK_S,		/* shared */	LOCK_X,		/* exclusive */	LOCK_AUTO_INC,	/* locks the auto-inc counter of a table in an exclusive mode */	LOCK_NONE,	/* this is used elsewhere to note consistent read */	LOCK_NUM = LOCK_NONE/* number of lock modes *
    
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn