You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: auto-increment.md
+76-16Lines changed: 76 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -297,46 +297,106 @@ After the value `2030000` is inserted, the next value is `2060001`. This jump in
297
297
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.
298
298
299
299
```sql
300
-
mysql>CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
300
+
CREATETABLEt(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
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.
317
327
318
328
```sql
319
-
mysql>DELETEFROM 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();
326
330
Query OK, 1 row affected (0.00 sec)
327
331
328
-
mysql>SELECT*FROM t;
332
+
SELECT*FROM t;
329
333
+-----+
330
334
| a |
331
335
+-----+
336
+
| 1 |
332
337
| 101 |
333
338
+-----+
334
-
1 rowinset (0.00 sec)
339
+
2 rowsinset (0.01 sec)
335
340
```
336
341
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`.
338
343
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
+
CREATETABLEt(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
0 commit comments