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: make the description of auto id conflicts problem more clear #758

Merged
merged 1 commit into from
Jun 7, 2018
Merged
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
19 changes: 11 additions & 8 deletions sql/mysql-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@ TiDB 的自增 ID (Auto Increment ID) 只保证自增且唯一,并不保证连

> **注意:**
>
> 在有多台 TiDB 使用自增 ID,建议不要混用缺省值和自定义值。因为目前在如下情况下会报错
>
> 在有两个 TiDB(TiDB A 缓存 [1,5000] 的自增 ID,TiDB B 缓存 [5001,10000] 的自增 ID)的集群,使用如下 SQL 语句创建一个带有自增 ID 的表:
> 在集群中有多个 tidb-server 实例时,如果表结构中有自增 ID,建议不要混用缺省值和自定义值。否则在如下情况下会遇到问题
>
> 假设有这样一个带有自增 ID 的表:
>
> ```
> create table t(id int unique key auto_increment, c int);
> ```
>
> TiDB 实现自增 ID 的原理是每个 tidb-server 实例缓存一段 ID 值用于分配,用完这段值再去取下一段。

> 假设集群中有两个 tidb-server 实例 A 和 B(A 缓存 [1,5000] 的自增 ID,B 缓存 [5001,10000] 的自增 ID),
>
> 该语句执行如下:
>
> 1. 客户端向 TiDB B 插入一条将 `id` 设置为 1 的语句,并执行成功。
> 2. 客户端向 TiDB A 发送插入一条记录,且记录中 `id` 使用缺省值即 1,最终返回 `Duplicated Error`。
> 依次执行操作:
>
> 该问题近期会解决。
> 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` 错误。


### 内建函数

Expand Down