-
Notifications
You must be signed in to change notification settings - Fork 710
add _tidb_rowid document
#22572
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
Open
tangenta
wants to merge
13
commits into
pingcap:master
Choose a base branch
from
tangenta:tangenta/tidb-rowid
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−5
Open
add _tidb_rowid document
#22572
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8c2c208
add _tidb_rowid document
tangenta 76ebbf2
Update tidb-rowid.md
tangenta bd6ecc9
Update tidb-rowid.md
tangenta 37096bf
Update tidb-rowid.md
tangenta e39783b
Apply suggestions from code review
hfxsd 62f24ee
Apply suggestions from code review
hfxsd b64082f
Apply suggestions from code review
hfxsd ebad53a
Merge remote-tracking branch 'upstream/master' into pr/22572
hfxsd 6b5ed14
Merge remote-tracking branch 'upstream/master' into pr/22572
hfxsd e15e9f0
Update tidb-rowid.md
hfxsd cd09cf6
Merge remote-tracking branch 'upstream/master' into pr/22572
hfxsd 82b1e29
Merge remote-tracking branch 'upstream/master' into pr/22572
hfxsd 22f67a5
Update tidb-rowid.md
hfxsd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| --- | ||
| title: _tidb_rowid | ||
| summary: Learn what `_tidb_rowid` is, when it is available, and how to use it safely. | ||
| --- | ||
|
|
||
| # _tidb_rowid | ||
|
|
||
| `_tidb_rowid` is a hidden system column that TiDB uses as the row handle for tables that do not use a clustered index. You cannot declare this column in the table schema, but you can reference it in SQL when the table uses `_tidb_rowid` as its handle. | ||
|
|
||
| In the current implementation, `_tidb_rowid` is an extra `BIGINT NOT NULL` handle column managed by TiDB. | ||
|
|
||
| > **Warning:** | ||
| > | ||
| > - Do not assume `_tidb_rowid` is globally unique in all cases. For partitioned tables that do not use clustered indexes, executing `ALTER TABLE ... EXCHANGE PARTITION` can leave different partitions with the same `_tidb_rowid` value. | ||
| > - If you need a stable unique identifier, define and use an explicit primary key instead of relying on `_tidb_rowid`. | ||
|
|
||
| ## When `_tidb_rowid` is available | ||
|
|
||
| `_tidb_rowid` is available for tables whose row handle is not a clustered primary key. In practice, this means the following table types use `_tidb_rowid`: | ||
|
|
||
| - Tables without primary keys | ||
| - Tables with primary keys that are explicitly defined as `NONCLUSTERED` | ||
|
|
||
| `_tidb_rowid` is not available for tables that use a clustered index, including the following: | ||
|
|
||
| - Tables with integer primary keys that are clustered row handles | ||
| - Tables with clustered indexes on composite primary keys | ||
|
|
||
| The following example shows the difference: | ||
|
|
||
| ```sql | ||
| CREATE TABLE t1 (a INT, b VARCHAR(20)); | ||
| CREATE TABLE t2 (id BIGINT PRIMARY KEY NONCLUSTERED, a INT); | ||
| CREATE TABLE t3 (id BIGINT PRIMARY KEY CLUSTERED, a INT); | ||
| ``` | ||
|
|
||
| For `t1` and `t2`, you can query `_tidb_rowid`: | ||
|
|
||
| ```sql | ||
| SELECT _tidb_rowid, a, b FROM t1; | ||
| SELECT _tidb_rowid, id, a FROM t2; | ||
| ``` | ||
|
|
||
| For `t3`, `_tidb_rowid` is unavailable because the clustered primary key is already the row handle: | ||
|
|
||
| ```sql | ||
| SELECT _tidb_rowid, id, a FROM t3; | ||
| ``` | ||
|
|
||
| ```sql | ||
| ERROR 1054 (42S22): Unknown column '_tidb_rowid' in 'field list' | ||
| ``` | ||
|
|
||
| ## Read `_tidb_rowid` | ||
|
|
||
| You can use `_tidb_rowid` in `SELECT` statements for supported tables. This is useful for tasks such as pagination, troubleshooting, and batch processing. | ||
|
|
||
| Example: | ||
|
|
||
| ```sql | ||
| CREATE TABLE t (a INT, b VARCHAR(20)); | ||
| INSERT INTO t VALUES (1, 'x'), (2, 'y'); | ||
|
|
||
| SELECT _tidb_rowid, a, b FROM t ORDER BY _tidb_rowid; | ||
| ``` | ||
|
|
||
| ```sql | ||
| +-------------+---+---+ | ||
| | _tidb_rowid | a | b | | ||
| +-------------+---+---+ | ||
| | 1 | 1 | x | | ||
| | 2 | 2 | y | | ||
| +-------------+---+---+ | ||
| ``` | ||
|
|
||
| To inspect the next value that TiDB will allocate for the row ID, use `SHOW TABLE ... NEXT_ROW_ID`: | ||
|
|
||
| ```sql | ||
| SHOW TABLE t NEXT_ROW_ID; | ||
| ``` | ||
|
|
||
| ```sql | ||
| +-----------------------+------------+-------------+--------------------+-------------+ | ||
| | DB_NAME | TABLE_NAME | COLUMN_NAME | NEXT_GLOBAL_ROW_ID | ID_TYPE | | ||
| +-----------------------+------------+-------------+--------------------+-------------+ | ||
| | update_doc_rowid_test | t | _tidb_rowid | 30001 | _TIDB_ROWID | | ||
| +-----------------------+------------+-------------+--------------------+-------------+ | ||
| ``` | ||
|
|
||
| ## Write `_tidb_rowid` | ||
|
|
||
| By default, TiDB does not allow `INSERT`, `REPLACE`, or `UPDATE` statements to write `_tidb_rowid` directly. | ||
|
|
||
| ```sql | ||
| INSERT INTO t(_tidb_rowid, a, b) VALUES (101, 4, 'w'); | ||
| ``` | ||
|
|
||
| ```sql | ||
| ERROR 1105 (HY000): insert, update and replace statements for _tidb_rowid are not supported | ||
| ``` | ||
|
|
||
| If you need to preserve row IDs during data import or migration, enable the system variable [`tidb_opt_write_row_id`](/system-variables.md#tidb_opt_write_row_id) first: | ||
|
|
||
| ```sql | ||
| SET @@tidb_opt_write_row_id = ON; | ||
| INSERT INTO t(_tidb_rowid, a, b) VALUES (100, 3, 'z'); | ||
| SET @@tidb_opt_write_row_id = OFF; | ||
|
|
||
| SELECT _tidb_rowid, a, b FROM t WHERE _tidb_rowid = 100; | ||
| ``` | ||
|
|
||
| ```sql | ||
| +-------------+---+---+ | ||
| | _tidb_rowid | a | b | | ||
| +-------------+---+---+ | ||
| | 100 | 3 | z | | ||
| +-------------+---+---+ | ||
| ``` | ||
|
|
||
| > **Warning:** | ||
| > | ||
| > `tidb_opt_write_row_id` is intended for import and migration scenarios. It is not recommended for regular application writes. | ||
|
|
||
| ## Restrictions | ||
|
|
||
| - You cannot create a user column named `_tidb_rowid`. | ||
| - You cannot rename an existing user column to `_tidb_rowid`. | ||
| - `_tidb_rowid` is an internal row handle. Do not treat it as a long-term business key. | ||
| - On partitioned non-clustered tables, `_tidb_rowid` values are not guaranteed to be unique across partitions. After you execute `EXCHANGE PARTITION`, different partitions can contain rows with the same `_tidb_rowid` value. | ||
| - Whether `_tidb_rowid` exists depends on the table layout. If a table uses a clustered index, use the primary key instead. | ||
|
|
||
| ## Address hotspot issues | ||
|
|
||
| For tables that use `_tidb_rowid`, TiDB allocates row IDs in increasing order by default. In write-intensive workloads, this can create write hotspots. | ||
|
|
||
| To mitigate this issue for tables that rely on implicit row IDs, consider using [`SHARD_ROW_ID_BITS`](/shard-row-id-bits.md) and, if needed, [`PRE_SPLIT_REGIONS`](/sql-statements/sql-statement-split-region.md#pre_split_regions). | ||
|
|
||
| Example: | ||
|
|
||
| ```sql | ||
| CREATE TABLE t ( | ||
| id BIGINT PRIMARY KEY NONCLUSTERED, | ||
| c INT | ||
| ) SHARD_ROW_ID_BITS = 4; | ||
| ``` | ||
|
|
||
| `SHARD_ROW_ID_BITS` applies only to tables that use the implicit row ID path. It does not apply to clustered-index tables. | ||
|
|
||
| ## Related statements and variables | ||
|
|
||
| - [`SHOW TABLE NEXT_ROW_ID`](/sql-statements/sql-statement-show-table-next-rowid.md): shows the next row ID that TiDB will allocate | ||
| - [`SHARD_ROW_ID_BITS`](/shard-row-id-bits.md): shards implicit row IDs to reduce hotspots | ||
| - [`Clustered Indexes`](/clustered-indexes.md): explains when a table uses the primary key instead of `_tidb_rowid` | ||
| - [`tidb_opt_write_row_id`](/system-variables.md#tidb_opt_write_row_id): controls whether writes to `_tidb_rowid` are allowed | ||
|
|
||
| ## See also | ||
|
|
||
| - [`CREATE TABLE`](/sql-statements/sql-statement-create-table.md) | ||
| - [`AUTO_INCREMENT`](/auto-increment.md) | ||
| - [Non-transactional DML](/non-transactional-dml.md) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.