From 0519f314aa98267c994beb69ec588c635cfbe157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Thu, 4 Nov 2021 13:36:36 +0100 Subject: [PATCH 1/9] tidb-functions: add info about special start_key/end_key values --- functions-and-operators/tidb-functions.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index f62051603a640..862c552fdf227 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -102,6 +102,29 @@ select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF33 1 row in set (0.001 sec) ``` +Tables have a special "infimum" start key and a special "supremum" end key. These can be recognized as they are only 36 characters long and end in 'F8'. `TIDB_DECODE_KEY` doesn't work on these special values. A small table only have a single region and only have these special keys as `START_KEY` and `END_KEY`. + +{{< copyable "sql" >}} + +```sql +SELECT TABLE_NAME, START_KEY, END_KEY FROM information_schema.TIKV_REGION_STATUS WHERE TABLE_NAME='stock' AND IS_INDEX=0; +``` + +```sql ++------------+--------------------------------------------------------+--------------------------------------------------------+ +| TABLE_NAME | START_KEY | END_KEY | ++------------+--------------------------------------------------------+--------------------------------------------------------+ +| stock | 7480000000000000FF4700000000000000F8 | 7480000000000000FF475F728000000000FF029DF00000000000FA | +| stock | 7480000000000000FF475F728000000000FF0D7ECA0000000000FA | 7480000000000000FF4900000000000000F8 | +| stock | 7480000000000000FF475F728000000000FF063E890000000000FA | 7480000000000000FF475F728000000000FF09E0350000000000FA | +| stock | 7480000000000000FF475F728000000000FF029DF00000000000FA | 7480000000000000FF475F728000000000FF063E890000000000FA | +| stock | 7480000000000000FF475F728000000000FF09E0350000000000FA | 7480000000000000FF475F728000000000FF0D7ECA0000000000FA | ++------------+--------------------------------------------------------+--------------------------------------------------------+ +5 rows in set (0.013 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. From 7957694fc3c8c31ea61f185f9157966297eed4aa Mon Sep 17 00:00:00 2001 From: Kolbe Kegel Date: Thu, 4 Nov 2021 12:18:23 -0700 Subject: [PATCH 2/9] Update tidb-functions.md clarified some language --- functions-and-operators/tidb-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 862c552fdf227..60b1e961d7c32 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -102,7 +102,7 @@ select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF33 1 row in set (0.001 sec) ``` -Tables have a special "infimum" start key and a special "supremum" end key. These can be recognized as they are only 36 characters long and end in 'F8'. `TIDB_DECODE_KEY` doesn't work on these special values. A small table only have a single region and only have these special keys as `START_KEY` and `END_KEY`. +Tables have a special "infimum" start key and a special "supremum" end key. These are only 18 bytes (36 characters) long and end in 'F8'. `TIDB_DECODE_KEY` doesn't work on these special values. A small table may only have a single region and only have these special keys as `START_KEY` and `END_KEY`. {{< copyable "sql" >}} From cd04e9bead82d87613a567d544c0fb51b4fee347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Mon, 17 Jan 2022 10:38:24 +0100 Subject: [PATCH 3/9] Rephrase --- functions-and-operators/tidb-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 60b1e961d7c32..712b899e69a06 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -102,7 +102,7 @@ select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF33 1 row in set (0.001 sec) ``` -Tables have a special "infimum" start key and a special "supremum" end key. These are only 18 bytes (36 characters) long and end in 'F8'. `TIDB_DECODE_KEY` doesn't work on these special values. A small table may only have a single region and only have these special keys as `START_KEY` and `END_KEY`. +The first region of a table will start with a key that only has the `table_id` of the table. The last region of the table ends with a `table_id` of the next table. Any regions in between will have longer keys that includes a `_tidb_rowid` or `handle`. {{< copyable "sql" >}} From 150f354f3498f54f30c8ec9b2914b8ff14f02d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Wed, 15 Nov 2023 17:15:28 +0100 Subject: [PATCH 4/9] Update functions-and-operators/tidb-functions.md Co-authored-by: Mattias Jonsson --- functions-and-operators/tidb-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 712b899e69a06..24e65847ad161 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -102,7 +102,7 @@ select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF33 1 row in set (0.001 sec) ``` -The first region of a table will start with a key that only has the `table_id` of the table. The last region of the table ends with a `table_id` of the next table. Any regions in between will have longer keys that includes a `_tidb_rowid` or `handle`. +The first region of a table will start 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 will have longer keys that includes a `_tidb_rowid` or `handle`. {{< copyable "sql" >}} From 7a50ce65e473ecfb986a7ea8018bacc6c890cb95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Mon, 27 Nov 2023 13:13:54 +0100 Subject: [PATCH 5/9] Apply suggestions from code review Co-authored-by: Aolin --- functions-and-operators/tidb-functions.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 7532c851fbd6f..6ac2fbd0da231 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -129,9 +129,7 @@ select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF33 1 row in set (0.001 sec) ``` -The first region of a table will start 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 will have longer keys that includes a `_tidb_rowid` or `handle`. - -{{< copyable "sql" >}} +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, START_KEY, END_KEY FROM information_schema.TIKV_REGION_STATUS WHERE TABLE_NAME='stock' AND IS_INDEX=0; From c62b4e09275ddeae9795b708415d0aae60b71055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Mon, 27 Nov 2023 13:18:05 +0100 Subject: [PATCH 6/9] Remove copyable --- functions-and-operators/tidb-functions.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 6ac2fbd0da231..f3de2e9230dd2 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,8 +66,6 @@ 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 ``` @@ -86,8 +82,6 @@ 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 ``` @@ -114,8 +108,6 @@ REPLICATIONSTATUS_STATEID: NULL 1 row in set (0.005 sec) ``` -{{< copyable "sql" >}} - ```sql select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF3338646232FF2D64FF3531632D3131FF65FF622D386337352DFFFF3830653635303138FFFF61396265000000FF00FB000000000000F9'); ``` @@ -232,8 +224,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"]'; @@ -303,8 +293,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); ``` @@ -322,8 +310,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)); ``` From 98016afc8fe4356acafb2194c4e9ee3c65b66990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Mon, 27 Nov 2023 13:20:31 +0100 Subject: [PATCH 7/9] Use upercase for SQL --- functions-and-operators/tidb-functions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index f3de2e9230dd2..8e7d617b6f519 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -67,7 +67,7 @@ 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. ```sql -show create table t2\G +SHOW CREATE TABLE t2\G ``` ```sql @@ -109,7 +109,7 @@ REPLICATIONSTATUS_STATEID: NULL ``` ```sql -select tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF3338646232FF2D64FF3531632D3131FF65FF622D386337352DFFFF3830653635303138FFFF61396265000000FF00FB000000000000F9'); +SELECT tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF3338646232FF2D64FF3531632D3131FF65FF622D386337352DFFFF3830653635303138FFFF61396265000000FF00FB000000000000F9'); ``` ```sql From 975edbcf520a7041450d7088c14c099e81acdd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 1 Dec 2023 08:48:51 +0100 Subject: [PATCH 8/9] Update based on review --- functions-and-operators/tidb-functions.md | 34 +++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 8e7d617b6f519..5e2033c3aef23 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -83,7 +83,7 @@ Create Table: CREATE TABLE `t2` ( ``` ```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 @@ -124,20 +124,30 @@ SELECT tidb_decode_key('7480000000000000FF3E5F720400000000FF0000000601633430FF33 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, START_KEY, END_KEY FROM information_schema.TIKV_REGION_STATUS WHERE TABLE_NAME='stock' AND IS_INDEX=0; +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 | START_KEY | END_KEY | -+------------+--------------------------------------------------------+--------------------------------------------------------+ -| stock | 7480000000000000FF4700000000000000F8 | 7480000000000000FF475F728000000000FF029DF00000000000FA | -| stock | 7480000000000000FF475F728000000000FF0D7ECA0000000000FA | 7480000000000000FF4900000000000000F8 | -| stock | 7480000000000000FF475F728000000000FF063E890000000000FA | 7480000000000000FF475F728000000000FF09E0350000000000FA | -| stock | 7480000000000000FF475F728000000000FF029DF00000000000FA | 7480000000000000FF475F728000000000FF063E890000000000FA | -| stock | 7480000000000000FF475F728000000000FF09E0350000000000FA | 7480000000000000FF475F728000000000FF0D7ECA0000000000FA | -+------------+--------------------------------------------------------+--------------------------------------------------------+ -5 rows in set (0.013 sec) ++------------+-----------------------------------------------------------+-----------------------------------------------------------+ +| 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. From ace71ecd0a63095bbdb448f514b375f165486640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 1 Dec 2023 08:50:35 +0100 Subject: [PATCH 9/9] Remove empty line --- functions-and-operators/tidb-functions.md | 1 - 1 file changed, 1 deletion(-) diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 5e2033c3aef23..ee86bd482b449 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -147,7 +147,6 @@ ORDER BY | 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.