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
+86-16Lines changed: 86 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -304,46 +304,116 @@ After the value `2030000` is inserted, the next value is `2060001`. This jump in
304
304
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.
305
305
306
306
```sql
307
-
mysql>CREATE TABLE t(a int AUTO_INCREMENT key) AUTO_ID_CACHE 100;
307
+
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:
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.
324
334
325
335
```sql
326
-
mysql>DELETEFROM 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();
333
337
Query OK, 1 row affected (0.00 sec)
334
338
335
-
mysql>SELECT*FROM t;
339
+
SELECT*FROM t;
336
340
+-----+
337
341
| a |
338
342
+-----+
343
+
| 1 |
339
344
| 101 |
340
345
+-----+
341
-
1 row inset (0.00 sec)
346
+
2 rows inset (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
+
<CustomContentplatform="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
+
<CustomContentplatform="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
+
CREATETABLEt(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 inset (0.01 sec)
342
390
```
343
391
344
-
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.
0 commit comments