diff --git a/functions-and-operators/tidb-functions.md b/functions-and-operators/tidb-functions.md index 14db38e2eac4b..ceeb249db4009 100644 --- a/functions-and-operators/tidb-functions.md +++ b/functions-and-operators/tidb-functions.md @@ -583,7 +583,45 @@ SELECT VITESS_HASH(123); ## TIDB_ENCODE_INDEX_KEY -Encodes an index key. +The `TIDB_ENCODE_INDEX_KEY()` function encodes a specified index key into a hexadecimal string. The syntax is as follows: + +```sql +TIDB_ENCODE_INDEX_KEY(, , , ..., ...) +``` + +Parameter descriptions: + +* ``: the name of the database that contains the target index. +* ``: the name of the table that contains the target index. For a partitioned table, you can specify the partition name, for example, `'t(p0)'`. +* ``: the name of the target index. +* `...`: the values of the index columns. You must specify the values in the same order as defined in the index. For a composite index, you must specify values for all index columns. +* `...`: the handle values for the row. The required handle values depend on the primary key type of the table: + + * If the table has no primary key, or the primary key is `NONCLUSTERED`, the handle value is the value of the hidden column `_tidb_rowid`. + * If the primary key is `CLUSTERED` and is a single-column integer, the handle value is the value of the primary key column. + * If the primary key is `CLUSTERED` and is a composite primary key or a non-integer type (common handle), the handle value consists of the values of all primary key columns in order. + +The following examples show how to call this function for the composite secondary index `idx(c1, c2)` under different primary key types. + +```sql +-- For tables without a primary key or with a NONCLUSTERED primary key, use the _tidb_rowid column. +SELECT TIDB_ENCODE_INDEX_KEY( + '', '', '', + , , <_tidb_rowid> +); + +-- For tables with a CLUSTERED integer primary key (the primary key column is id), use the id column. +SELECT TIDB_ENCODE_INDEX_KEY( + '', '', '', + , , +); + +-- For tables with a CLUSTERED composite primary key (the primary key columns are p1, p2), provide the values of p1 and p2 in their defined order. +SELECT TIDB_ENCODE_INDEX_KEY( + '', '', '', + , , , +); +``` ```sql CREATE TABLE t(id int PRIMARY KEY, a int, KEY `idx` (a)); @@ -594,7 +632,7 @@ Query OK, 0 rows affected (0.00 sec) ``` ```sql -INSERT INTO t VALUES(1,1); +INSERT INTO t VALUES(1,2); ``` ``` @@ -602,21 +640,31 @@ Query OK, 1 row affected (0.00 sec) ``` ```sql -SELECT TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 1, 1); +SELECT TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 2, 1); ``` ``` +----------------------------------------------------------------------------+ -| TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 1, 1) | +| TIDB_ENCODE_INDEX_KEY('test', 't', 'idx', 2, 1) | +----------------------------------------------------------------------------+ -| 74800000000000007f5f698000000000000001038000000000000001038000000000000001 | +| 7480000000000000b45f698000000000000001038000000000000002038000000000000001 | +----------------------------------------------------------------------------+ 1 row in set (0.00 sec) ``` ## TIDB_ENCODE_RECORD_KEY -Encodes a record key. +The `TIDB_ENCODE_RECORD_KEY()` function encodes a specified row record key into a hexadecimal string. The function syntax is as follows: + +```sql +TIDB_ENCODE_RECORD_KEY(, , ...) +``` + +Parameter descriptions: + +* ``: the name of the database that contains the target table. +* ``: the name of the target table. For a partitioned table, you can specify the partition name in ``, for example, `'t(p0)'`. +* `...`: the handle (row key) values for the corresponding row. The exact composition of the handle depends on the primary key type of the table, such as whether the primary key is `CLUSTERED`, a common handle, or uses the hidden column `_tidb_rowid`. For more information, see the description of `...` in [`TIDB_ENCODE_INDEX_KEY()`](#tidb_encode_index_key). ```sql CREATE TABLE t(id int PRIMARY KEY, a int, KEY `idx` (a)); @@ -627,7 +675,7 @@ Query OK, 0 rows affected (0.00 sec) ``` ```sql -INSERT INTO t VALUES(1,1); +INSERT INTO t VALUES(1,2); ``` ```