AUTO_INCREMENT
This document introduces the AUTO_INCREMENT
column attribute, including its concept, implementation principles, auto-increment related features, and restrictions.
Concept
AUTO_INCREMENT
is a column attribute that is used to automatically fill in default column values. When the INSERT
statement does not specify values for the AUTO_INCREMENT
column, the system automatically assigns values to this column.
For performance reasons, AUTO_INCREMENT
numbers are allocated in a batch of values (30 thousand by default) to each TiDB server. This means that while AUTO_INCREMENT
numbers are guaranteed to be unique, values assigned to an INSERT
statement will only be monotonic on a per TiDB server basis.
The following is a basic example of AUTO_INCREMENT
:
CREATE TABLE t(id int PRIMARY KEY AUTO_INCREMENT, c int);
INSERT INTO t(c) VALUES (1);
INSERT INTO t(c) VALUES (2);
INSERT INTO t(c) VALUES (3), (4), (5);
mysql> SELECT * FROM t;
+----+---+
| id | c |
+----+---+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+----+---+
5 rows in set (0.01 sec)
In addition, AUTO_INCREMENT
also supports the INSERT
statements that explicitly specify column values. In such cases, TiDB stores the explicitly specified values:
INSERT INTO t(id, c) VALUES (6, 6);
mysql> SELECT * FROM t;
+----+---+
| id | c |
+----+---+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
+----+---+
6 rows in set (0.01 sec)
The usage above is the same as that of AUTO_INCREMENT
in MySQL. However, in terms of the specific value that is implicitly assigned, TiDB differs from MySQL significantly.
Implementation principles
TiDB implements the AUTO_INCREMENT
implicit assignment in the following way:
For each auto-increment column, a globally visible key-value pair is used to record the maximum ID that has been assigned. In a distributed environment, communication between nodes has some overhead. Therefore, to avoid the issue of write amplification, each TiDB node applies for a batch of consecutive IDs as caches when assigning IDs, and then applies for the next batch of IDs after the first batch is assigned. Therefore, TiDB nodes do not apply to the storage node for IDs when assigning IDs each time. For example:
CREATE TABLE t(id int UNIQUE KEY AUTO_INCREMENT, c int);
Assume two TiDB instances, A
and B
, in the cluster. If you execute an INSERT
statement on the t
table on A
and B
respectively:
INSERT INTO t (c) VALUES (1)
Instance A
might cache the auto-increment IDs of [1,30000]
, and instance B
might cache the auto-increment IDs of [30001,60000]
. In INSERT
statements to be executed, these cached IDs of each instance will be assigned to the AUTO_INCREMENT
column as the default values.
Basic Features
Uniqueness
In the example above, perform the following operations in order:
The client inserts a statement
INSERT INTO t VALUES (2, 1)
to instanceB
, which setsid
to2
. The statement is successfully executed.The client sends a statement
INSERT INTO t (c) (1)
to instanceA
. This statement does not specify the value ofid
, so the ID is assigned byA
. At present, becauseA
caches the IDs of[1, 30000]
, it might assign2
as the value of the auto-increment ID, and increases the local counter by1
. At this time, the data whose ID is2
already exists in the database, so theDuplicated Error
error is returned.
Monotonicity
TiDB guarantees that AUTO_INCREMENT
values are monotonic (always increasing) on a per-server basis. Consider the following example where consecutive AUTO_INCREMENT
values of 1-3 are generated:
CREATE TABLE t (a int PRIMARY KEY AUTO_INCREMENT, b timestamp NOT NULL DEFAULT NOW());
INSERT INTO t (a) VALUES (NULL), (NULL), (NULL);
SELECT * FROM t;
Query OK, 0 rows affected (0.11 sec)
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
+---+---------------------+
| a | b |
+---+---------------------+
| 1 | 2020-09-09 20:38:22 |
| 2 | 2020-09-09 20:38:22 |
| 3 | 2020-09-09 20:38:22 |
+---+---------------------+
3 rows in set (0.00 sec)
The AUTO_INCREMENT
sequence might appear to jump dramatically if an INSERT
operation is performed against a different TiDB server. This is caused by the fact that each server has its own cache of AUTO_INCREMENT
values:
INSERT INTO t (a) VALUES (NULL);
SELECT * FROM t;
Query OK, 1 row affected (0.03 sec)
+---------+---------------------+
| a | b |
+---------+---------------------+
| 1 | 2020-09-09 20:38:22 |
| 2 | 2020-09-09 20:38:22 |
| 3 | 2020-09-09 20:38:22 |
| 2000001 | 2020-09-09 20:43:43 |
+---------+---------------------+
4 rows in set (0.00 sec)
A new INSERT
operation against the initial TiDB server generates the AUTO_INCREMENT
value of 4
. This is because the initial TiDB server still has space left in the AUTO_INCREMENT
cache for allocation. In this case, the sequence of values cannot be considered globally monotonic, because the value of 4
is inserted after the value of 2000001
:
mysql> INSERT INTO t (a) VALUES (NULL);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM t ORDER BY b;
+---------+---------------------+
| a | b |
+---------+---------------------+
| 1 | 2020-09-09 20:38:22 |
| 2 | 2020-09-09 20:38:22 |
| 3 | 2020-09-09 20:38:22 |
| 2000001 | 2020-09-09 20:43:43 |
| 4 | 2020-09-09 20:44:43 |
+---------+---------------------+
5 rows in set (0.00 sec)
The AUTO_INCREMENT
cache does not persist across TiDB server restarts. The following INSERT
statement is performed after the initial TiDB server is restarted:
mysql> INSERT INTO t (a) VALUES (NULL);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM t ORDER BY b;
+---------+---------------------+
| a | b |
+---------+---------------------+
| 1 | 2020-09-09 20:38:22 |
| 2 | 2020-09-09 20:38:22 |
| 3 | 2020-09-09 20:38:22 |
| 2000001 | 2020-09-09 20:43:43 |
| 4 | 2020-09-09 20:44:43 |
| 2030001 | 2020-09-09 20:54:11 |
+---------+---------------------+
6 rows in set (0.00 sec)
A high rate of TiDB server restarts might contribute to the exhaustion of AUTO_INCREMENT
values. In the above example, the initial TiDB server still has values [5-30000]
free in its cache. These values are lost, and will not be reallocated.
It is not recommended to rely onAUTO_INCREMENT
values being continuous. Consider the following example, where a TiDB server has a cache of values [2000001-2030000]
. By manually inserting the value 2029998
, you can see the behavior as a new cache range is retrieved:
mysql> INSERT INTO t (a) VALUES (2029998);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO t (a) VALUES (NULL);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO t (a) VALUES (NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO t (a) VALUES (NULL);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO t (a) VALUES (NULL);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM t ORDER BY b;
+---------+---------------------+
| a | b |
+---------+---------------------+
| 1 | 2020-09-09 20:38:22 |
| 2 | 2020-09-09 20:38:22 |
| 3 | 2020-09-09 20:38:22 |
| 2000001 | 2020-09-09 20:43:43 |
| 4 | 2020-09-09 20:44:43 |
| 2030001 | 2020-09-09 20:54:11 |
| 2029998 | 2020-09-09 21:08:11 |
| 2029999 | 2020-09-09 21:08:11 |
| 2030000 | 2020-09-09 21:08:11 |
| 2060001 | 2020-09-09 21:08:11 |
| 2060002 | 2020-09-09 21:08:11 |
+---------+---------------------+
11 rows in set (0.00 sec)
After the value 2030000
is inserted, the next value is 2060001
. This jump in sequence is due to another TiDB server obtaining the intermediate cache range of [2030001-2060000]
. When multiple TiDB servers are deployed, there will be gaps in the AUTO_INCREMENT
sequence because cache requests are interleaved.
Cache size control
In earlier versions of TiDB, the cache size of the auto-increment ID was transparent to users. Starting from v3.0.14, v3.1.2, and v4.0.rc-2, TiDB has introduced the AUTO_ID_CACHE
table option to allow users to set the cache size for allocating the auto-increment ID.
mysql> CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO t values();
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t;
+---+
| a |
+---+
| 1 |
+---+
1 row in set (0.01 sec)
At this time, if you invalidate the auto-increment cache of this column and redo the implicit insertion, the result is as follows:
mysql> DELETE FROM t;
Query OK, 1 row affected (0.01 sec)
mysql> RENAME TABLE t to t1;
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO t1 values()
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t;
+-----+
| a |
+-----+
| 101 |
+-----+
1 row in set (0.00 sec)
The re-assigned value is 101
. This shows that the size of cache for allocating the auto-increment ID is 100
.
In addition, when the length of consecutive IDs in a batch INSERT
statement exceeds the length of AUTO_ID_CACHE
, TiDB increases the cache size accordingly to ensure that the statement can be inserted properly.
Auto-increment step size and offset
Starting from v3.0.9 and v4.0.0-rc.1, similar to the behavior of MySQL, the value implicitly assigned to the auto-increment column is controlled by the @@auto_increment_increment
and @@auto_increment_offset
session variables.
The value (ID) implicitly assigned to auto-increment columns satisfies the following equation:
(ID - auto_increment_offset) % auto_increment_increment == 0
Restrictions
Currently, AUTO_INCREMENT
has the following restrictions when used in TiDB:
- It must be defined on the column of the primary key or unique index.
- It must be defined on the column of
INTEGER
,FLOAT
, orDOUBLE
type. - It cannot be specified on the same column with the
DEFAULT
column value. ALTER TABLE
cannot be used to add theAUTO_INCREMENT
attribute.ALTER TABLE
can be used to remove theAUTO_INCREMENT
attribute. However, starting from v2.1.18 and v3.0.4, TiDB uses the session variable@@tidb_allow_remove_auto_inc
to control whetherALTER TABLE MODIFY
orALTER TABLE CHANGE
can be used to remove theAUTO_INCREMENT
attribute of a column. By default, you cannot useALTER TABLE MODIFY
orALTER TABLE CHANGE
to remove theAUTO_INCREMENT
attribute.