Skip to content

Commit c519b96

Browse files
authored
auto-increment.md: add invalidate cache descriptions (#18851) (#19202)
1 parent 2db0347 commit c519b96

File tree

1 file changed

+76
-16
lines changed

1 file changed

+76
-16
lines changed

auto-increment.md

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -297,46 +297,106 @@ After the value `2030000` is inserted, the next value is `2060001`. This jump in
297297
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.
298298

299299
```sql
300-
mysql> CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
300+
CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
301301
Query OK, 0 rows affected (0.02 sec)
302302

303-
mysql> INSERT INTO t values();
303+
INSERT INTO t values();
304304
Query OK, 1 row affected (0.00 sec)
305-
Records: 1 Duplicates: 0 Warnings: 0
306305

307-
mysql> SELECT * FROM t;
306+
SELECT * FROM t;
308307
+---+
309308
| a |
310309
+---+
311310
| 1 |
312311
+---+
313312
1 row in set (0.01 sec)
313+
314+
SHOW CREATE TABLE t;
315+
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
316+
| Table | Create Table |
317+
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
318+
| t | CREATE TABLE `t` (
319+
`a` int(11) NOT NULL AUTO_INCREMENT,
320+
PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */
321+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=101 /*T![auto_id_cache] AUTO_ID_CACHE=100 */ |
322+
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
323+
1 row in set (0.00 sec)
314324
```
315325

316-
At this time, if you invalidate the auto-increment cache of this column and redo the implicit insertion, the result is as follows:
326+
At this time, if you restart TiDB, the auto-increment ID cache will be lost, and new insert operations will allocate IDs starting from a higher value beyond the previously cached range.
317327

318328
```sql
319-
mysql> DELETE FROM t;
320-
Query OK, 1 row affected (0.01 sec)
321-
322-
mysql> RENAME TABLE t to t1;
323-
Query OK, 0 rows affected (0.01 sec)
324-
325-
mysql> INSERT INTO t1 values()
329+
INSERT INTO t VALUES();
326330
Query OK, 1 row affected (0.00 sec)
327331

328-
mysql> SELECT * FROM t;
332+
SELECT * FROM t;
329333
+-----+
330334
| a |
331335
+-----+
336+
| 1 |
332337
| 101 |
333338
+-----+
334-
1 row in set (0.00 sec)
339+
2 rows in set (0.01 sec)
335340
```
336341

337-
The re-assigned value is `101`. This shows that the size of cache for allocating the auto-increment ID is `100`.
342+
The newly allocated value is `101`. This shows that the size of cache for allocating auto-increment IDs is `100`.
338343

339-
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.
344+
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 insert data properly.
345+
346+
### Clear the auto-increment ID cache
347+
348+
In some scenarios, you might need to clear the auto-increment ID cache to ensure data consistency. For example:
349+
350+
- In the scenario of incremental replication using [Data Migration (DM)](https://docs.pingcap.com/tidb/v6.1/dm-overview), once the replication is complete, data writing to the downstream TiDB switches from DM to your application's write operations. Meanwhile, the ID writing mode of the auto-increment column usually switches from explicit insertion to implicit allocation.
351+
- When your application involves both explicit ID insertion and implicit ID allocation, you need to clear the auto-increment ID cache to avoid conflicts between future implicitly allocated IDs and previously explicitly inserted IDs, which could result in primary key conflict errors. For more information, see [Uniqueness](/auto-increment.md#uniqueness).
352+
353+
To clear the auto-increment ID cache on all TiDB nodes in the cluster, you can execute the `ALTER TABLE` statement with `AUTO_INCREMENT = 0`. For example:
354+
355+
```sql
356+
CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
357+
Query OK, 0 rows affected (0.02 sec)
358+
359+
INSERT INTO t VALUES();
360+
Query OK, 1 row affected (0.02 sec)
361+
362+
INSERT INTO t VALUES(50);
363+
Query OK, 1 row affected (0.00 sec)
364+
365+
SELECT * FROM t;
366+
+----+
367+
| a |
368+
+----+
369+
| 1 |
370+
| 50 |
371+
+----+
372+
2 rows in set (0.01 sec)
373+
```
374+
375+
```sql
376+
ALTER TABLE t AUTO_INCREMENT = 0;
377+
Query OK, 0 rows affected, 1 warning (0.07 sec)
378+
379+
SHOW WARNINGS;
380+
+---------+------+-------------------------------------------------------------------------+
381+
| Level | Code | Message |
382+
+---------+------+-------------------------------------------------------------------------+
383+
| Warning | 1105 | Can't reset AUTO_INCREMENT to 0 without FORCE option, using 101 instead |
384+
+---------+------+-------------------------------------------------------------------------+
385+
1 row in set (0.01 sec)
386+
387+
INSERT INTO t VALUES();
388+
Query OK, 1 row affected (0.02 sec)
389+
390+
SELECT * FROM t;
391+
+-----+
392+
| a |
393+
+-----+
394+
| 1 |
395+
| 50 |
396+
| 101 |
397+
+-----+
398+
3 rows in set (0.01 sec)
399+
```
340400
341401
### Auto-increment step size and offset
342402

0 commit comments

Comments
 (0)