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

reference/sql: add some details to the document of auto_random #2478

Merged
merged 5 commits into from
Mar 18, 2020

Conversation

tangenta
Copy link
Contributor

@tangenta tangenta commented Mar 16, 2020

What is changed, added or deleted? (Required)

Add some details to the document of auto_random.

Which TiDB version(s) do your changes apply to? (Required)

  • master (the latest development version, including v4.0 changes for now)
  • v3.1 (TiDB 3.1 versions)
  • v3.0 (TiDB 3.0 versions)
  • v2.1 (TiDB 2.1 versions)

If you select two or more versions from above, to trigger the bot to cherry-pick this PR to your desired release version branch(es), you must add corresponding labels such as needs-cherry-pick-3.1, needs-cherry-pick-3.0, and needs-cherry-pick-2.1.

What is the related PR or file link(s)?

pingcap/tidb#15145: fix last_insert_id in auto_random mode
pingcap/tidb#15262: show max allocate times when an auto_random table is created

  • This PR is translated from:
  • Other reference link(s):

@tangenta
Copy link
Contributor Author

@bb7133 @TomShawn PTAL~

@TomShawn TomShawn self-assigned this Mar 16, 2020
@TomShawn TomShawn added the translation/doing This PR’s assignee is translating this PR. label Mar 16, 2020
Copy link
Contributor

@TomShawn TomShawn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM
@bb7133 PTAL

+ 如果该 `INSERT` 语句没有指定整型主键列(`a` 列)的值,TiDB 会为该列自动分配值。该值不保证自增,不保证连续,只保证唯一,避免了连续的行 ID 带来的热点问题。
+ 如果该 `INSERT` 语句显式指定了整型主键列的值,和 `AUTO_INCREMENT` 属性类似,TiDB 会保存该值。
+ 如果该 `INSERT` 语句没有指定整型主键列(`a` 列)的值,或者指定为 `NULL`,TiDB 会为该列自动分配值。该值不保证自增,不保证连续,只保证唯一,避免了连续的行 ID 带来的热点问题。
+ 如果该 `INSERT` 语句显式指定了整型主键列的值,和 `AUTO_INCREMENT` 属性类似,TiDB 会保存该值。注意,如果系统变量 `@@sql_mode` 中没有设置 `NO_AUTO_VALUE_ON_ZERO`, 即使显式指定为 0,TiDB 也会为该列自动分配值。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ 如果该 `INSERT` 语句显式指定了整型主键列的值,和 `AUTO_INCREMENT` 属性类似,TiDB 会保存该值。注意,如果系统变量 `@@sql_mode` 中没有设置 `NO_AUTO_VALUE_ON_ZERO`即使显式指定为 0,TiDB 也会为该列自动分配值。
+ 如果该 `INSERT` 语句显式指定了整型主键列的值,和 `AUTO_INCREMENT` 属性类似,TiDB 会保存该值。注意,如果未在系统变量 `@@sql_mode` 中设置 `NO_AUTO_VALUE_ON_ZERO`即使显式指定整型主键列的值为 `0`,TiDB 也会为该列自动分配值。


自动分配值的计算方式如下:

该行值在二进制形式下的最高五位(称为 shard bits)由当前事务的开始时间决定,剩下的位数按照自增顺序分配数值
该行值在二进制形式下,除去最高位(无论是 unsigned 还是 signed)的次高 5 位(称为 shard bits)由当前事务的开始时间决定,剩下的位数按照自增的顺序分配
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
该行值在二进制形式下,除去最高位(无论是 unsigned 还是 signed)的次高 5 位(称为 shard bits)由当前事务的开始时间决定,剩下的位数按照自增的顺序分配。
该行值在二进制形式下,除去最高位(无论是 unsigned 还是 signed)的次高五位(称为 shard bits)由当前事务的开始时间决定,剩下的位数按照自增的顺序分配。


另外,含有 `AUTO_RANDOM` 属性的表在系统表 `information_schema.tables` 中 `TIDB_ROW_ID_SHARDING_INFO` 一列的值为 `PK_AUTO_RANDOM_BITS=x`,其中 `x` 为 shard bits 的数量。

要获取上一次 TiDB 隐式分配的 id,可以使用 `select last_insert_id()` 查看,例如:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
要获取上一次 TiDB 隐式分配的 id,可以使用 `select last_insert_id()` 查看,例如:
要获取上一次 TiDB 隐式分配的 ID,可以使用 `select last_insert_id()` 查看,例如:

Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TomShawn TomShawn added the status/LGT2 Indicates that a PR has LGTM 2. label Mar 18, 2020
Copy link
Contributor

@TomShawn TomShawn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TomShawn TomShawn added the status/can-merge Indicates a PR has been approved by a committer. label Mar 18, 2020
@sre-bot
Copy link
Contributor

sre-bot commented Mar 18, 2020

Your auto merge job has been accepted, waiting for 2512, 2513, 2514

@sre-bot
Copy link
Contributor

sre-bot commented Mar 18, 2020

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Mar 18, 2020

cherry pick to release-3.1 in PR #2516

@TomShawn TomShawn added translation/done This PR has been translated from English into Chinese and updated to pingcap/docs-cn in a PR. and removed translation/doing This PR’s assignee is translating this PR. labels Mar 20, 2020
rleungx pushed a commit to rleungx/docs-cn that referenced this pull request May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. translation/done This PR has been translated from English into Chinese and updated to pingcap/docs-cn in a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants