Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto_increment: add steps and notices to auto_increment and auto_random #19116

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions auto-increment.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ SELECT * FROM t;
3 rows in set (0.01 sec)
```

Lightning 在导入完数据之后,会自动清除自增 ID 缓存。但 DM 和 TiCDC 在增量同步数据之后不会自动清除,因此需要在停止 DM 和 TiCDC 之后、进行主备切换之前,手动清除下游集群的自增 ID 缓存。

### 自增步长和偏移量设置

从 v3.0.9 和 v4.0.rc-1 开始,和 MySQL 的行为类似,自增列隐式分配的值遵循 session 变量 `@@auto_increment_increment` 和 `@@auto_increment_offset` 的控制,其中自增列隐式分配的值 (ID) 将满足式子 `(ID - auto_increment_offset) % auto_increment_increment == 0`。
Expand Down
19 changes: 19 additions & 0 deletions auto-random.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ SHOW WARNINGS;

`AUTO_RANDOM` 列隐式分配的值和自增列类似,也遵循 session 变量 [`auto_increment_increment`](/system-variables.md#auto_increment_increment) 和 [`auto_increment_offset`](/system-variables.md#auto_increment_offset) 的控制,其中隐式分配值的自增位 (ID) 满足等式 `(ID - auto_increment_offset) % auto_increment_increment == 0`。

## 清除自增 ID 缓存

显式插入 `AUTO_RANDOM` 列的行为与 `AUTO_INCREMENT` 列一致,你也需要清除自增 ID 缓存,详细信息请参阅[清除自增 ID 缓存](/auto-increment.md#清除自增-id-缓存)。

你可以执行 `ALTER TABLE` 语句设置 `AUTO_RANDOM_BASE = 0` 来清除集群中所有 TiDB 节点的自增 ID 缓存。例如:

```sql
ALTER TABLE t AUTO_RANDOM_BASE=0;
Query OK, 0 rows affected, 1 warning (0.52 sec)

SHOW WARNINGS;
+---------+------+-------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------------------------------+
| Warning | 1105 | Can't reset AUTO_INCREMENT to 0 without FORCE option, using 101 instead |
+---------+------+-------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

## 使用限制

目前在 TiDB 中使用 `AUTO_RANDOM` 有以下限制:
Expand Down