步長
-
auto_increment_increment
controls the interval between successive column values.mysql> SHOW VARIABLES LIKE 'auto_inc%';
偏移量
-
auto_increment_offset
determines the starting point for theAUTO_INCREMENT
column value. Consider the following, assuming that these statements are executed during the same session as the example given in the description forauto_increment_increment
:mysql> SHOW VARIABLES LIKE 'auto_inc%';
When the value of
auto_increment_offset
is greater than that ofauto_increment_increment
, the value ofauto_increment_offset
is ignored.
具體的偏移量算法
InnoDB
無法在執(zhí)行Insert
語句之前知道確切的插入記錄數(shù),因此會使用表級的AUTO_INC
鎖(該鎖比較特殊,并不像通常的鎖那樣妇汗,在事務(wù)結(jié)束時釋放避乏,而是在該語句執(zhí)行完畢后釋放)勉耀。對于AUTO_INCREMENT
值佩厚,目前InnoDB
會采取預(yù)分配的策略骤素,即首先分配1创倔,如果用盡則double
,如果用盡再double
琢歇,即1兰怠,2梦鉴,4,8...
揭保。需要注意的是肥橙,如果innodb_autoinc_lock_mode =2
,那么InnoDB
不會使用AUTO_INC
鎖秸侣。
# 插入1行
n = 1
AUTO_INCREMENT += auto_increment_increment * 1
# 插入2行
n = 2
AUTO_INCREMENT += auto_increment_increment * 2
# 插入10行
n = 16
AUTO_INCREMENT += auto_increment_increment * 8
總結(jié)
公司的數(shù)據(jù)庫不連續(xù)的原因是應(yīng)為集群里面的數(shù)據(jù)庫的機(jī)器數(shù)量是5存筏,導(dǎo)致會有很多相差為5的主鍵。
參考
關(guān)于mysql的自增型主鍵的更多其他描述味榛,見http://whitesock.iteye.com/blog/1329857