2016-09-09 86 views
0

當我從tableB向tableA中插入一些行時,它將像往常一樣工作。插入行的auto_increment鍵'id'是正常的,但下一個auto_increment'id'是一個例外。在我的情況下,SQL的結果表明,受總行的是5504和插入的有效行是5504,但下一個「身份證」是8192,應該是5505「插入到tableA(col1,col2)從tableB」選擇col3,col4後出現mysql auto_increment錯誤「

下面我的表結構:

-- ---------------------------- 
-- Table structure for t_ds 
-- ---------------------------- 
DROP TABLE IF EXISTS `t_ds`; 
CREATE TABLE `t_ds` (
    `id` bigint(64) NOT NULL AUTO_INCREMENT, 
    `deviceId` varchar(50) DEFAULT NULL, 
    `eventId` varchar(50) DEFAULT NULL, 
    `eventTime` bigint(64) DEFAULT NULL, 
    `logInfoId` bigint(64) DEFAULT NULL, 
    `statusCode` tinyint(1) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `index_ds` (`deviceId`,`eventId`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

和SQL插入語句:

INSERT INTO t_ds(deviceId, eventId, eventTime, logInfoId, statusCode) 
SELECT deviceId, eventId, eventTime, id, statusCode FROM t_loginfo 
WHERE (deviceId, eventId, eventTime) in (
    SELECT 
     deviceId, eventId, MAX(eventTime) 
    FROM 
     t_loginfo 
    GROUP BY deviceId, eventId 
) 
GROUP BY deviceId, eventId; 

我不知道爲什麼增加鍵「ID」是一個奇怪的次數,所以我做了什麼努力尋找原因。首先,我刪除table't_ds',重建它並在上面的sql insert語句中追加sql'limit *,1',逐行插入行,奇怪的num消失,這是正常的。

所以我爲此做了一些實驗。

1. delete table 't_ds' , rebuild it. 
2. run the SQL insert statement with limit *,*. 

結果:

limit 0,1: insert 1 row, total rows is 1, the last id is 1, the next id is 2; 
limit 1,2: insert 2 row, total rows is 3, the last id is 3, the next id is 5; 
limit 3,3: insert 3 row, total rows is 6, the last id is 7, the next id is 8; 
limit 6,4: insert 4 row, total rows is 10, the last id is 11, the next id is 15; 
limit 10,5: insert 5 row, total rows is 15, the last id is 19, the next id is 22; 

ID的序列:

1 2 3 5 6 7 8 9 10 11 15 16 17 18 19 

另一個怪異的情況是插入的行NUM是5000+或6000+,無論兩個下的id是8192.

+0

不要擔心價值。儘管它的名字,AUTO_INCREMENT功能並不能保證值是遞增的,只是它們是唯一的。 – Strawberry

+0

重要嗎?從表中刪除不會更改下一個返回的自動增量值。您需要刪除它並重新創建它。 – e4c5

+0

[身份列值突然跳到1001在sql服務器]可能重複(http://stackoverflow.com/questions/17587094/identity-column-value-suddenly-jumps-to-1001-in-sql-server) –

回答

0

這是一個非常普遍的問題。這通常發生在您重新啓動SQL Server時。當我們處於開發服務器時,這類問題就來了。它在生產服務器中很少發生。因爲生產服務器不經常重新啓動。

+0

你仔細看過這個問題嗎? – LuoYingYing

+0

是的,我仔細閱讀你的問題。我面臨這樣的問題。有很多關於這個的articals,當你谷歌。如果你會通過這個藝術,你會明白我想說什麼。 http://www.codeproject.com/Tips/668042/SQL-Server-2012-Auto-Identity-Column-Value-Jump-Is –

+0

感謝您提供有關身份列值問題的案例。這兩起案件之間存在巨大差異。在我的情況下,在重啓MySQL服務器服務後,整個過程中沒有重新啓動,最後一個ID正常。 – LuoYingYing

相關問題