From 7d3813d73d72788fa4088635601b99702630a2e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Wed, 6 Dec 2023 02:55:19 +0100 Subject: [PATCH] tidb-functions: add info about special start_key/end_key values (#6728) --- functions-and-operators/tidb-functions.md | 50 +++++++++++++++-------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index c853d5b632c89..ee86bd482b449 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -53,8 +53,6 @@ This section provides examples for some of the functions above. In the following example, the table `t1` has a hidden `rowid` that is generated by TiDB. The `TIDB_DECODE_KEY` is used in the statement. From the result, you can see that the hidden `rowid` is decoded and output, which is a typical result for the non-clustered primary key. -{{< copyable "sql" >}} - ```sql SELECT START_KEY, TIDB_DECODE_KEY(START_KEY) FROM information_schema.tikv_region_status WHERE table_name='t1' AND REGION_ID=2\G ``` @@ -68,10 +66,8 @@ TIDB_DECODE_KEY(START_KEY): {"_tidb_rowid":1958897,"table_id":"59"} In the following example, the table `t2` has a compound clustered primary key. From the JSON output, you can see a `handle` that contains the name and value for both of the columns that are part of the primary key. -{{< copyable "sql" >}} - ```sql -show create table t2\G +SHOW CREATE TABLE t2\G ``` ```sql @@ -86,10 +82,8 @@ Create Table: CREATE TABLE `t2` ( 1 row in set (0.001 sec) ``` -{{< copyable "sql" >}} - ```sql -select * from information_schema.tikv_region_status where table_name='t2' limit 1\G +SELECT * FROM information_schema.tikv_region_status WHERE table_name='t2' LIMIT 1\G ``` ```sql @@ -114,10 +108,8 @@ REPLICATIONSTATUS_STATEID: NULL 1 row in set (0.005 sec) ``` -{{< copyable "sql" >}} - ```sql -select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF3338646232FF2D64FF3531632D3131FF65FF622D386337352DFFFF3830653635303138FFFF61396265000000FF00FB000000000000F9'); +SELECT tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF3338646232FF2D64FF3531632D3131FF65FF622D386337352DFFFF3830653635303138FFFF61396265000000FF00FB000000000000F9'); ``` ```sql @@ -129,6 +121,36 @@ select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF33 1 row in set (0.001 sec) ``` +The first Region of a table starts with a key that only has the `table_id` of the table. The last Region of the table ends with `table_id + 1`. Any Regions in between have longer keys that includes a `_tidb_rowid` or `handle`. + +```sql +SELECT + TABLE_NAME, + TIDB_DECODE_KEY(START_KEY), + TIDB_DECODE_KEY(END_KEY) +FROM + information_schema.TIKV_REGION_STATUS +WHERE + TABLE_NAME='stock' + AND IS_INDEX=0 +ORDER BY + START_KEY; +``` + +```sql ++------------+-----------------------------------------------------------+-----------------------------------------------------------+ +| TABLE_NAME | TIDB_DECODE_KEY(START_KEY) | TIDB_DECODE_KEY(END_KEY) | ++------------+-----------------------------------------------------------+-----------------------------------------------------------+ +| stock | {"table_id":143} | {"handle":{"s_i_id":"32485","s_w_id":"3"},"table_id":143} | +| stock | {"handle":{"s_i_id":"32485","s_w_id":"3"},"table_id":143} | {"handle":{"s_i_id":"64964","s_w_id":"5"},"table_id":143} | +| stock | {"handle":{"s_i_id":"64964","s_w_id":"5"},"table_id":143} | {"handle":{"s_i_id":"97451","s_w_id":"7"},"table_id":143} | +| stock | {"handle":{"s_i_id":"97451","s_w_id":"7"},"table_id":143} | {"table_id":145} | ++------------+-----------------------------------------------------------+-----------------------------------------------------------+ +4 rows in set (0.031 sec) +``` + +`TIDB_DECODE_KEY` returns valid JSON on success and retuns the argument value if it fails to decode. + ### TIDB_DECODE_PLAN You can find TiDB execution plans in encoded form in the slow query log. The `TIDB_DECODE_PLAN()` function is then used to decode the encoded plans into a human-readable form. @@ -211,8 +233,6 @@ This function returns a string, which is in the format of a JSON string array. T > * This function has a high overhead. In queries with a large number of rows (for example, querying the full table of `information_schema.cluster_tidb_trx` on a large and busy cluster), using this function might cause the queries to run for too long. Use it with caution. > * This function has a high overhead because every time it is called, it internally queries the `STATEMENTS_SUMMARY`, `STATEMENTS_SUMMARY_HISTORY`, `CLUSTER_STATEMENTS_SUMMARY`, and `CLUSTER_STATEMENTS_SUMMARY_HISTORY` tables, and the query involves the `UNION` operation. This function currently does not support vectorization, that is, when calling this function for multiple rows of data, the above query is performed separately for each row. -{{< copyable "sql" >}} - ```sql set @digests = '["e6f07d43b5c21db0fbb9a31feac2dc599787763393dd5acbfad80e247eb02ad5","38b03afa5debbdf0326a014dbe5012a62c51957f1982b3093e748460f8b00821","e5796985ccafe2f71126ed6c0ac939ffa015a8c0744a24b7aee6d587103fd2f7"]'; @@ -282,8 +302,6 @@ The following example shows how to use the `TIDB_SHARD` function. The following statement shows how to use the `TIDB_SHARD` function to calculate the SHARD value of `12373743746`: - {{< copyable "sql" >}} - ```sql SELECT TIDB_SHARD(12373743746); ``` @@ -301,8 +319,6 @@ The following example shows how to use the `TIDB_SHARD` function. - Create a shard index using the `TIDB_SHARD` function: - {{< copyable "sql" >}} - ```sql CREATE TABLE test(id INT PRIMARY KEY CLUSTERED, a INT, b INT, UNIQUE KEY uk((tidb_shard(a)), a)); ```