一次auto_increment 引發的血案!

先說說這次的起因,開發寫的SQL,三次連續寫入數據,每次6條數據,寫了3次,中間的pk竟然有跳過的現象。很是納悶。QQ空間裏面一些人討論了一會,今天找到了原因,寫下來僅供參考。

參考http://dinglin.iteye.com/blog/1279536 

http://dev.mysql.com/doc/refman/5.6/en/innodb-auto-increment-configurable.html


具體的數據我就不寫了,上面的blog說的很明白了。

貼一個代碼吧


inline ulonglong

compute_next_insert_id(ulonglong nr,struct system_variables *variables)

{

  const ulonglong save_nr= nr;


  if (variables->auto_increment_increment == 1)

    nr= nr + 1; // optimization of the formula below

  else

  {

    nr= (((nr+ variables->auto_increment_increment -

           variables->auto_increment_offset)) /

         (ulonglong) variables->auto_increment_increment);

    nr= (nr* (ulonglong) variables->auto_increment_increment +

         variables->auto_increment_offset);

  }


  if (unlikely(nr <= save_nr))

    return ULONGLONG_MAX;


  return nr;

}


算是學習了這個自增原理的,還一個自增鎖。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章