Search code examples
mysqlinnodb

MySQL auto increment column


I see a weird behavior where my auto-increment column number is only increasing in a step of 2 instead of 1. So I end up with row ids as 1, 3, 5, 7. I use MySQL 5.6 + InnoDB as the engine. Any idea why this weirdness?

mysql> select version();
+-----------------+
| version()       |
+-----------------+
| 5.6.20-68.0-log |
+-----------------+
1 row in set (0.00 sec)

mysql> show create table temp_table;
| superset_version | CREATE TABLE `temp_table` (
  `_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'comm1',
  `name` varchar(100) NOT NULL COMMENT 'comm2',
  `start_time` bigint(20) NOT NULL COMMENT 'comm3',
  `updated_at` bigint(20) NOT NULL COMMENT 'comm4',
  `status` varchar(50) NOT NULL COMMENT 'comm5',
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 |

Notice that insert incremented the _id column by a difference of 2.

mysql> insert into superset_version(name, start_time, updated_at, status) value("TEMP ROW", -1, -1, "erro");
    Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from superset_version order by _id desc limit 3;
+-----+----------+------------+------------+--------+
| _id | name     | start_time | updated_at | status |
+-----+----------+------------+------------+--------+
|  33 | TEMP ROW |         -1 |         -1 | erro   |
|  31 | TEMP ROW |         -1 |         -1 | erro   |
|  29 | TEMP ROW |         -1 |         -1 | erro   |
+-----+----------+------------+------------+--------+
3 rows in set (0.00 sec)

Solution

  • auto_increment_increment setting is most likely set to 2, therefore mysql increases auto increment numbers by 2. Use show varibles like ... command to check the setting.