Skip to content

Conversation

@TomShawn
Copy link
Contributor

@TomShawn TomShawn commented Feb 26, 2020

What is changed, added or deleted?

Add a user document for the auto_random feature.

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

Important Notice:

If your changes apply to multiple TiDB documentation versions, to trigger the bot to cherry-pick this PR to other release branches, make sure you add labels such as:

  • needs-cherry-pick-3.1

@TomShawn TomShawn added dev translation/from-docs-cn This PR is translated from a PR in pingcap/docs-cn. v4.0 This PR/issue applies to TiDB v4.0. labels Feb 26, 2020
@TomShawn
Copy link
Contributor Author

@tangenta PTAL, thanks!

Copy link
Contributor

@tangenta tangenta 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 requested a review from yikeke March 3, 2020 02:12

- Determines whether to allow using `AUTO_RANDOM`.
- Default value: `false`
- By default, TiDB does not supporting using `AUTO_RANDOM`. When the value is `true`, you cannot set `alter-primary-key` to `true` at the same time.
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
- By default, TiDB does not supporting using `AUTO_RANDOM`. When the value is `true`, you cannot set `alter-primary-key` to `true` at the same time.
- By default, TiDB does not support using `AUTO_RANDOM`. When the value is `true`, you cannot set `alter-primary-key` to `true` at the same time.


> **Warning:**
>
> `AUTO_RANDOM` is still an experimental feature. It is recommended that you **DO NOT** use the attribute in the production environment. In later TiDB versions, the syntax or semantics of `AUTO_RANDOM` might change.
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
> `AUTO_RANDOM` is still an experimental feature. It is recommended that you **DO NOT** use the attribute in the production environment. In later TiDB versions, the syntax or semantics of `AUTO_RANDOM` might change.
> `AUTO_RANDOM` is still an experimental feature. It is **NOT** recommended that you use the attribute in the production environment. In later TiDB versions, the syntax or semantics of `AUTO_RANDOM` might change.


## User scenario

When you write data intensively into TiDB which has the table with a primary key of the auto-increment integer type, hotspot problem might occur. To solve the hotspot problem in this scenario, you can use the `AUTO_RANDOM` attribute. Refer to [Highly Concurrent Write Best Practices](/reference/best-practices/high-concurrency.md#complex-hotspot-problems) for details.
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
When you write data intensively into TiDB which has the table with a primary key of the auto-increment integer type, hotspot problem might occur. To solve the hotspot problem in this scenario, you can use the `AUTO_RANDOM` attribute. Refer to [Highly Concurrent Write Best Practices](/reference/best-practices/high-concurrency.md#complex-hotspot-problems) for details.
When you write data intensively into TiDB and TiDB has the table with a primary key of the auto-increment integer type, a hotspot issue might occur. To solve this issue, you can use the `AUTO_RANDOM` attribute. Refer to [Highly Concurrent Write Best Practices](/reference/best-practices/high-concurrency.md#complex-hotspot-problems) for details.


When you write data intensively into TiDB which has the table with a primary key of the auto-increment integer type, hotspot problem might occur. To solve the hotspot problem in this scenario, you can use the `AUTO_RANDOM` attribute. Refer to [Highly Concurrent Write Best Practices](/reference/best-practices/high-concurrency.md#complex-hotspot-problems) for details.

Take the following `CREATE TABLE` statement as an example:
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
Take the following `CREATE TABLE` statement as an example:
Take the following created table as an example:

create table t (a int primary key auto_increment, b varchar(255))
```

After creating a table by executing the above statement, execute a large number of `INSERT` statements on the newly-created table. These `INSERT` statements do not specify the values of the primary key. See the following example:
Copy link
Contributor

@yikeke yikeke Mar 3, 2020

Choose a reason for hiding this comment

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

Suggested change
After creating a table by executing the above statement, execute a large number of `INSERT` statements on the newly-created table. These `INSERT` statements do not specify the values of the primary key. See the following example:
On this `t` table, you execute a large number of `INSERT` statements that do not specify the values of the primary key as below:

Then execute the `INSERT` statement such as `INSERT INTO t(b) values...`.

+ If the `INSERT` statement does not specify the values of the integer primary key column (column `a`), TiDB automatically assigns values to this column. These values are not necessarily auto-increment or continuous but are unique, which avoids the hotspot problem caused by continuous row IDs.
+ If the `INSERT` statement explicitly specifies the values of the integer primary key column, TiDB saves these values as they are, which works similarly to the `AUTO_INCREMENT` attribute.
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
+ If the `INSERT` statement explicitly specifies the values of the integer primary key column, TiDB saves these values as they are, which works similarly to the `AUTO_INCREMENT` attribute.
+ If the `INSERT` statement explicitly specifies the values of the integer primary key column, TiDB saves these values, which works similarly to the `AUTO_INCREMENT` attribute.


TiDB automatically assigns values in the following way:

The highest five digits of the row value in binary (namely, shard bits) is determined by the starting time of the current transaction. The remaining digits are assigned values in an auto-increment way.
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
The highest five digits of the row value in binary (namely, shard bits) is determined by the starting time of the current transaction. The remaining digits are assigned values in an auto-increment way.
The highest five digits of the row value in binary (namely, shard bits) are determined by the starting time of the current transaction. The remaining digits are assigned values in an auto-increment order.


In the above `CREATE TABLE` statement, `3` shard bits are specified. The range of the number of shard bits is `[1, field_max_bits)`. `field_max_bits` is the length of bits occupied by the primary key column.

In the `TIDB_ROW_ID_SHARDING_INFO` row of the `information_schema.tables` system table, the value of the table with the `AUTO_RANDOM` attribute is `PK_AUTO_RANDOM_BITS=x`. In this value, `x` is the number of shard bits.
Copy link
Contributor

@yikeke yikeke Mar 3, 2020

Choose a reason for hiding this comment

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

Suggested change
In the `TIDB_ROW_ID_SHARDING_INFO` row of the `information_schema.tables` system table, the value of the table with the `AUTO_RANDOM` attribute is `PK_AUTO_RANDOM_BITS=x`. In this value, `x` is the number of shard bits.
For tables with the `AUTO_RANDOM` attribute, the value of the `TIDB_ROW_ID_SHARDING_INFO` column in the `information_schema.tables` system table is `PK_AUTO_RANDOM_BITS=x`. `x` is the number of shard bits.

Please confirm with the author whether the following understanding of the original Chinese is right:
对于含有 AUTO_RANDOM 属性的表,在系统表 information_schema.tables 的 TIDB_ROW_ID_SHARDING_INFO 列的值为。。。

@TomShawn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is correct.


The above two statements have the same meaning.

In the result of `show create table`, the `AUTO_RANDOM` attribute is commented out. This comment includes a version number (for example, `/*T!30100 auto_random */`). `30100` in the example indicates that the `AUTO_RANDOM` attribute is introduced in v3.1.0. TiDB of lower versions ignore the `AUTO_RANDOM` attribute in the above comment.
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
In the result of `show create table`, the `AUTO_RANDOM` attribute is commented out. This comment includes a version number (for example, `/*T!30100 auto_random */`). `30100` in the example indicates that the `AUTO_RANDOM` attribute is introduced in v3.1.0. TiDB of lower versions ignore the `AUTO_RANDOM` attribute in the above comment.
In the result of `show create table`, the `AUTO_RANDOM` attribute is commented out. This comment includes a version number (for example, `/*T!30100 auto_random */`). Here `30100` indicates that the `AUTO_RANDOM` attribute is introduced in v3.1.0. TiDB of a lower version ignores the `AUTO_RANDOM` attribute in the above comment.


In the result of `show create table`, the `AUTO_RANDOM` attribute is commented out. This comment includes a version number (for example, `/*T!30100 auto_random */`). `30100` in the example indicates that the `AUTO_RANDOM` attribute is introduced in v3.1.0. TiDB of lower versions ignore the `AUTO_RANDOM` attribute in the above comment.

This attribute supports forward compatibility, namely, downgrade compatibility. TiDB earlier than v3.1.0 ignores the `AUTO_RANDOM` attribute of a table (with the above comment), so the table can be used by TiDB of earlier versions.
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
This attribute supports forward compatibility, namely, downgrade compatibility. TiDB earlier than v3.1.0 ignores the `AUTO_RANDOM` attribute of a table (with the above comment), so the table can be used by TiDB of earlier versions.
This attribute supports forward compatibility, namely, downgrade compatibility. TiDB earlier than v3.1.0 ignores the `AUTO_RANDOM` attribute of a table (with the above comment), so TiDB of earlier versions can also use the table with the attribute.

- You cannot change the column type of the primary key column that is specified with `AUTO_RANDOM` attribute.
- You cannot specify `AUTO_RANDOM` and `AUTO_INCREMENT` for the same column at the same time.
- You cannot specify `AUTO_RANDOM` and `DEFAULT` (the default value of a column) for the same column at the same time.
- It is recommended that you do not explicitly specify a value for the row with the `AUTO_RANDOM` attribute when you insert data. Otherwise, the numeral values to be automatically assigned might be used up in advance.
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
- It is recommended that you do not explicitly specify a value for the row with the `AUTO_RANDOM` attribute when you insert data. Otherwise, the numeral values to be automatically assigned might be used up in advance.
- It is **not** recommended that you explicitly specify a value for the column with the `AUTO_RANDOM` attribute when you insert data. Otherwise, the numeral values that can be automatically assigned for this table might be used up in advance.

@TomShawn
Copy link
Contributor Author

TomShawn commented Mar 3, 2020

@yikeke Comments addressed, PTAL again, thanks!

Copy link
Contributor

@yikeke yikeke 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 3, 2020
@sre-bot
Copy link
Contributor

sre-bot commented Mar 3, 2020

/run-all-tests

@sre-bot sre-bot merged commit 316dcae into pingcap:master Mar 3, 2020
sre-bot pushed a commit to sre-bot/docs that referenced this pull request Mar 3, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
@sre-bot
Copy link
Contributor

sre-bot commented Mar 3, 2020

cherry pick to release-3.1 in PR #1932

@TomShawn TomShawn deleted the auto-random branch March 3, 2020 06:50
yikeke pushed a commit that referenced this pull request Mar 3, 2020
)

* cherry pick #1875 to release-3.1

Signed-off-by: sre-bot <sre-bot@pingcap.com>

* solve conflicts

Co-authored-by: TomShawn <41534398+TomShawn@users.noreply.github.com>
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. translation/from-docs-cn This PR is translated from a PR in pingcap/docs-cn. v4.0 This PR/issue applies to TiDB v4.0.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants