Skip to content

Commit 5bdbdb8

Browse files
cherry pick #4116 to release-4.0 (#4119)
Signed-off-by: ti-srebot <[email protected]> Co-authored-by: spencerkee <[email protected]>
1 parent 81a0bab commit 5bdbdb8

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

auto-increment.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ The following is a basic example of `AUTO_INCREMENT`:
1919
{{< copyable "sql" >}}
2020

2121
```sql
22-
create table t(id int primary key AUTO_INCREMENT, c int);
22+
CREATE TABLE t(id int PRIMARY KEY AUTO_INCREMENT, c int);
2323
```
2424

2525
{{< copyable "sql" >}}
2626

2727
```sql
28-
insert into t(c) values (1);
29-
insert into t(c) values (2);
30-
insert into t(c) values (3), (4), (5);
28+
INSERT INTO t(c) VALUES (1);
29+
INSERT INTO t(c) VALUES (2);
30+
INSERT INTO t(c) VALUES (3), (4), (5);
3131
```
3232

3333
```sql
34-
mysql> select * from t;
34+
mysql> SELECT * FROM t;
3535
+----+---+
3636
| id | c |
3737
+----+---+
@@ -49,11 +49,11 @@ In addition, `AUTO_INCREMENT` also supports the `INSERT` statements that explici
4949
{{< copyable "sql" >}}
5050

5151
```sql
52-
insert into t(id, c) values (6, 6);
52+
INSERT INTO t(id, c) VALUES (6, 6);
5353
```
5454

5555
```sql
56-
mysql> select * from t;
56+
mysql> SELECT * FROM t;
5757
+----+---+
5858
| id | c |
5959
+----+---+
@@ -76,13 +76,13 @@ TiDB implements the `AUTO_INCREMENT` implicit assignment in the following way:
7676
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:
7777

7878
```sql
79-
create table t(id int unique key AUTO_INCREMENT, c int);
79+
CREATE TABLE t(id int UNIQUE KEY AUTO_INCREMENT, c int);
8080
```
8181

8282
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:
8383

8484
```sql
85-
insert into t (c) values (1)
85+
INSERT INTO t (c) VALUES (1)
8686
```
8787

8888
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.
@@ -97,9 +97,9 @@ Instance `A` might cache the auto-increment IDs of `[1,30000]`, and instance `B`
9797
9898
In the example above, perform the following operations in order:
9999

100-
1. The client inserts a statement `insert into t values (2, 1)` to instance `B`, which sets `id` to `2`. The statement is successfully executed.
100+
1. The client inserts a statement `INSERT INTO t VALUES (2, 1)` to instance `B`, which sets `id` to `2`. The statement is successfully executed.
101101

102-
2. The client sends a statement `insert into t (c) (1)` to instance `A`. This statement does not specify the value of `id`, so the ID is assigned by `A`. At present, because `A` caches the IDs of `[1, 30000]`, it might assign `2` as the value of the auto-increment ID, and increases the local counter by `1`. At this time, the data whose ID is `2` already exists in the database, so the `Duplicated Error` error is returned.
102+
2. The client sends a statement `INSERT INTO t (c) (1)` to instance `A`. This statement does not specify the value of `id`, so the ID is assigned by `A`. At present, because `A` caches the IDs of `[1, 30000]`, it might assign `2` as the value of the auto-increment ID, and increases the local counter by `1`. At this time, the data whose ID is `2` already exists in the database, so the `Duplicated Error` error is returned.
103103

104104
### Monotonicity
105105

@@ -108,7 +108,7 @@ TiDB guarantees that `AUTO_INCREMENT` values are monotonic (always increasing) o
108108
{{< copyable "sql" >}}
109109

110110
```sql
111-
CREATE TABLE t (a int primary key AUTO_INCREMENT, b timestamp NOT NULL DEFAULT NOW());
111+
CREATE TABLE t (a int PRIMARY KEY AUTO_INCREMENT, b timestamp NOT NULL DEFAULT NOW());
112112
INSERT INTO t (a) VALUES (NULL), (NULL), (NULL);
113113
SELECT * FROM t;
114114
```
@@ -237,14 +237,14 @@ After the value `2030000` is inserted, the next value is `2060001`. This jump in
237237
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.
238238

239239
```sql
240-
mysql> create table t(a int auto_increment key) AUTO_ID_CACHE 100;
240+
mysql> CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
241241
Query OK, 0 rows affected (0.02 sec)
242242

243-
mysql> insert into t values();
243+
mysql> INSERT INTO t values();
244244
Query OK, 1 row affected (0.00 sec)
245245
Records: 1 Duplicates: 0 Warnings: 0
246246

247-
mysql> select * from t;
247+
mysql> SELECT * FROM t;
248248
+---+
249249
| a |
250250
+---+
@@ -256,16 +256,16 @@ mysql> select * from t;
256256
At this time, if you invalidate the auto-increment cache of this column and redo the implicit insertion, the result is as follows:
257257

258258
```sql
259-
mysql> delete from t;
259+
mysql> DELETE FROM t;
260260
Query OK, 1 row affected (0.01 sec)
261261

262-
mysql> rename table t to t1;
262+
mysql> RENAME TABLE t to t1;
263263
Query OK, 0 rows affected (0.01 sec)
264264

265-
mysql> insert into t1 values()
265+
mysql> INSERT INTO t1 values()
266266
Query OK, 1 row affected (0.00 sec)
267267

268-
mysql> select * from t;
268+
mysql> SELECT * FROM t;
269269
+-----+
270270
| a |
271271
+-----+
@@ -276,7 +276,7 @@ mysql> select * from t;
276276

277277
The re-assigned value is `101`. This shows that the size of cache for allocating the auto-increment ID is `100`.
278278

279-
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.
279+
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.
280280

281281
### Auto-increment step size and offset
282282

0 commit comments

Comments
 (0)