Skip to content

Commit 0226633

Browse files
authored
auto-increment.md: add invalidate cache descriptions (#18851)
1 parent 885cc3e commit 0226633

File tree

1 file changed

+86
-16
lines changed

1 file changed

+86
-16
lines changed

auto-increment.md

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,46 +304,116 @@ After the value `2030000` is inserted, the next value is `2060001`. This jump in
304304
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.
305305

306306
```sql
307-
mysql> CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
307+
CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
308308
Query OK, 0 rows affected (0.02 sec)
309309

310-
mysql> INSERT INTO t values();
310+
INSERT INTO t values();
311311
Query OK, 1 row affected (0.00 sec)
312-
Records: 1 Duplicates: 0 Warnings: 0
313312

314-
mysql> SELECT * FROM t;
313+
SELECT * FROM t;
315314
+---+
316315
| a |
317316
+---+
318317
| 1 |
319318
+---+
320319
1 row in set (0.01 sec)
320+
321+
SHOW CREATE TABLE t;
322+
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
323+
| Table | Create Table |
324+
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
325+
| t | CREATE TABLE `t` (
326+
`a` int(11) NOT NULL AUTO_INCREMENT,
327+
PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */
328+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=101 /*T![auto_id_cache] AUTO_ID_CACHE=100 */ |
329+
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
330+
1 row in set (0.00 sec)
321331
```
322332

323-
At this time, if you invalidate the auto-increment cache of this column and redo the implicit insertion, the result is as follows:
333+
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.
324334

325335
```sql
326-
mysql> DELETE FROM t;
327-
Query OK, 1 row affected (0.01 sec)
328-
329-
mysql> RENAME TABLE t to t1;
330-
Query OK, 0 rows affected (0.01 sec)
331-
332-
mysql> INSERT INTO t1 values()
336+
INSERT INTO t VALUES();
333337
Query OK, 1 row affected (0.00 sec)
334338

335-
mysql> SELECT * FROM t;
339+
SELECT * FROM t;
336340
+-----+
337341
| a |
338342
+-----+
343+
| 1 |
339344
| 101 |
340345
+-----+
341-
1 row in set (0.00 sec)
346+
2 rows in set (0.01 sec)
347+
```
348+
349+
The newly allocated value is `101`. This shows that the size of cache for allocating auto-increment IDs is `100`.
350+
351+
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.
352+
353+
### Clear the auto-increment ID cache
354+
355+
In some scenarios, you might need to clear the auto-increment ID cache to ensure data consistency. For example:
356+
357+
<CustomContent platform="tidb">
358+
359+
- In the scenario of incremental replication using [Data Migration (DM)](/dm/dm-overview.md), 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.
360+
361+
</CustomContent>
362+
<CustomContent platform="tidb-cloud">
363+
364+
- In the scenario of incremental replication using the [Data Migration](/tidb-cloud/migrate-incremental-data-from-mysql-using-data-migration.md) feature, 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.
365+
366+
</CustomContent>
367+
368+
- 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).
369+
370+
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:
371+
372+
```sql
373+
CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
374+
Query OK, 0 rows affected (0.02 sec)
375+
376+
INSERT INTO t VALUES();
377+
Query OK, 1 row affected (0.02 sec)
378+
379+
INSERT INTO t VALUES(50);
380+
Query OK, 1 row affected (0.00 sec)
381+
382+
SELECT * FROM t;
383+
+----+
384+
| a |
385+
+----+
386+
| 1 |
387+
| 50 |
388+
+----+
389+
2 rows in set (0.01 sec)
342390
```
343391

344-
The re-assigned value is `101`. This shows that the size of cache for allocating the auto-increment ID is `100`.
392+
```sql
393+
ALTER TABLE t AUTO_INCREMENT = 0;
394+
Query OK, 0 rows affected, 1 warning (0.07 sec)
395+
396+
SHOW WARNINGS;
397+
+---------+------+-------------------------------------------------------------------------+
398+
| Level | Code | Message |
399+
+---------+------+-------------------------------------------------------------------------+
400+
| Warning | 1105 | Can't reset AUTO_INCREMENT to 0 without FORCE option, using 101 instead |
401+
+---------+------+-------------------------------------------------------------------------+
402+
1 row in set (0.01 sec)
403+
404+
INSERT INTO t VALUES();
405+
Query OK, 1 row affected (0.02 sec)
345406
346-
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.
407+
SELECT * FROM t;
408+
+-----+
409+
| a |
410+
+-----+
411+
| 1 |
412+
| 50 |
413+
| 101 |
414+
+-----+
415+
3 rows in set (0.01 sec)
416+
```
347417
348418
### Auto-increment step size and offset
349419

0 commit comments

Comments
 (0)