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

sql: update the step of the "Auto Increment ID" #842

Merged
merged 2 commits into from
Aug 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions sql/mysql-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ TiDB 的自增 ID (Auto Increment ID) 只保证自增且唯一,并不保证连
> create table t(id int unique key auto_increment, c int);
> ```
>
> TiDB 实现自增 ID 的原理是每个 tidb-server 实例缓存一段 ID 值用于分配,用完这段值再去取下一段。
> TiDB 实现自增 ID 的原理是每个 tidb-server 实例缓存一段 ID 值用于分配(目前会缓存 30000 个 ID),用完这段值再去取下一段。

> 假设集群中有两个 tidb-server 实例 A 和 B(A 缓存 [1,5000] 的自增 ID,B 缓存 [5001,10000] 的自增 ID),
> 假设集群中有两个 tidb-server 实例 A 和 B(A 缓存 [1,30000] 的自增 ID,B 缓存 [30001,60000] 的自增 ID),
>
> 依次执行操作:
>
> 1. 客户端向 B 插入一条将 `id` 设置为 1 的语句 `insert into t values (1, 1)`,并执行成功。
> 2. 客户端向 A 发送 Insert 语句 `insert into t (c) (1)`,这条语句中没有指定 `id` 的值,所以会由 A 分配,当前 A 缓存了 [1, 5000] 这段 ID,所以会分配 1 为自增 ID 的值,并把本地计数器加 1。而此时数据库中已经存在 `id` 为 1 的数据,最终返回 `Duplicated Error` 错误。
> 2. 客户端向 A 发送 Insert 语句 `insert into t (c) (1)`,这条语句中没有指定 `id` 的值,所以会由 A 分配,当前 A 缓存了 [1, 30000] 这段 ID,所以会分配 1 为自增 ID 的值,并把本地计数器加 1。而此时数据库中已经存在 `id` 为 1 的数据,最终返回 `Duplicated Error` 错误。


### 内建函数
Expand Down