筍子工作雜記

十二月 24, 2006

使用 transaction 的疑問

Filed under: Database — shinnlu @ 5:15 下午

這幾天剛好要實作一個程式,這是一隻資料庫程式,而資料庫是使用 Mysql。程式本身沒什麼好講的,但是卻需要使用 Transaction,突然想起很久以前使用 Mysql + Transaction 碰到的問題,連忙翻出舊的電腦記錄,在這裡直接貼上來:
=====
Q:使用 transaction 的疑問:
一定要用 InnoDB、BDB 才能用 Trans,但 InnoDB 不支援 AUTO_INCREMENT 在多個 Key 裡,
所以有 AUTO_INCREMENT multi-key table只能用 BDB,但 dreamhost 不支援 BDB!!!!!!!!!!
A:自己抓 AUTO_INCREMENT 值
=====
這個問題是在 Mysql Server 3.23.49 上測試的,而 InnoDB 在 Mysql 5.0 己經 AUTO_INCREMENT 欄位,但是 InnoDB 的 AUTO_INCREMENT 是每次向資料庫查詢而得來,而這個值並不會寫入磁碟,而是存在記憶體裡,引述一段官方文件:
If you specify an AUTO_INCREMENT column for an InnoDB table, the table handle in the InnoDB data dictionary contains a special counter called the auto-increment counter that is used in assigning new values for the column. This counter is stored only in main memory, not on disk.

InnoDB uses the following algorithm to initialize the auto-increment counter for a table T that contains an AUTO_INCREMENT column named ai_col: After a server startup, for the first insert into a table T, InnoDB executes the equivalent of this statement:
SELECT MAX(ai_col) FROM T FOR UPDATE;

無迴響 »

仍無迴響。

此篇文章迴響的訂閱源料 TrackBack URL

發表迴響

Powered by WordPress