How to use HINTs (for versions 5.2 and earlier)
This topic describes how to use HINTs in versions 5.2 and earlier.
Basic syntax
/!TDDL:hint command*/Custom HINTs in PolarDB-X 1.0 use MySQL comments. This means that a custom HINT statement for PolarDB-X 1.0 is placed between /! and */ and must start with TDDL:. In this statement, hint command is the custom HINT command for PolarDB-X 1.0 and corresponds to a specific operation.
For example, the following SQL statement uses a custom HINT in PolarDB-X 1.0 to display the table names of each database shard.
/!TDDL:SCAN*/SHOW TABLES;In this SQL statement, /!TDDL:SCAN*/ is the custom HINT for PolarDB-X 1.0. It starts with TDDL:, and SCAN is the custom HINT command for PolarDB-X 1.0.
Read/write splitting
Data synchronization between a primary RDS instance and a read-only instance has a millisecond-level delay. Because of this delay, you must send read queries to the primary instance to read data immediately after it is modified. To meet this requirement, PolarDB-X 1.0 provides custom read/write splitting HINTs to specify whether to send SQL statements to the primary instance or a read-only instance.
Syntax
/!TDDL:MASTER|SLAVE*/You can use this custom HINT to specify whether an SQL statement is executed on the primary instance or on a read-only instance. For the /!TDDL:SLAVE*/ custom HINT, if a primary RDS instance has multiple read-only instances, PolarDB-X 1.0 randomly selects a read-only instance to execute the SQL statement based on the allocated weights.
Examples
- Execute an SQL statement on the primary instance:
/!TDDL:MASTER*/SELECT * FROM table_name;Adding the custom HINT
/!TDDL:MASTER*/before an SQL statement sends the statement to the primary instance for execution. - Execute an SQL statement on a read-only instance:
/!TDDL:SLAVE*/SELECT * FROM table_name;After you add the custom HINT
/!TDDL:SLAVE*/before an SQL statement, the statement is randomly sent to a read-only instance for execution based on the allocated weights.
- This read/write splitting custom HINT takes effect only for read SQL statements that are not in a transaction. If the SQL statement is a write statement or is in a transaction, the statement is sent to the RDS primary instance for execution.
- PolarDB-X 1.0
/*!TDDL:SLAVE*/custom HINT, PolarDB-X 1.0 randomly selects a read-only instance based on weights and sends the SQL statement for execution. If no read-only instances exist, an error is not reported. Instead, the primary instance executes the statement.
Read-only instance delay cutoff
If you configure read-only instances for the RDS primary instance of a PolarDB-X 1.0 database and distribute read traffic between the primary and read-only instances, PolarDB-X 1.0 sends SQL statements to either the primary instance or the read-only instances for execution based on the read/write ratio. However, if there is significant latency in the asynchronous data replication between the primary instance and the read-only instances, sending SQL statements to the read-only instances for execution can cause errors or return incorrect results.
The read-only instance delay cutoff feature determines whether to send an SQL statement to the primary instance or a read-only instance based on the maximum primary-secondary replication delay.
Syntax
/!TDDL:SQL_DELAY_CUTOFF=time*/You can specify a value for SQL_DELAY_CUTOFF in a custom HINT. When the SQL_DELAY value (MySQL primary-secondary replication delay) of the secondary database reaches or exceeds the time value (in seconds), search statements are sent to the primary instance.
Examples
- Set the primary-secondary replication delay to 5 seconds:
/!TDDL:SQL_DELAY_CUTOFF=5*/SELECT * FROM table_name;In the SQL statement, the value of
SQL_DELAY_CUTOFFis set to 5. When the secondary database'sSQL_DELAYvalue reaches or exceeds 5 seconds, the search statement is sent to the primary instance for execution. - Use with other custom HINTs:
/!TDDL:SLAVE AND SQL_DELAY_CUTOFF=5*/SELECT * FROM table_name;The secondary database delay cutoff HINT can also be used with other HINTs. By default, this SQL query is sent to a read-only instance. However, if the primary-secondary replication delay reaches or exceeds 5 seconds, the request is sent to the primary instance.
Custom SQL timeout
In PolarDB-X 1.0, the default SQL execution timeout for PolarDB-X 1.0 nodes and RDS is 900 seconds. This value can be adjusted. However, some slow SQL statements may run for longer than 900 seconds. For these statements, PolarDB-X 1.0 provides a custom HINT that lets you adjust the timeout.
Syntax
/!TDDL:SOCKET_TIMEOUT=time*/The unit of SOCKET_TIMEOUT is milliseconds. You can use this HINT to adjust the timeout of SQL statements as needed.
Example
Set the SQL timeout to 40 seconds:
/!TDDL:SOCKET_TIMEOUT=40000*/SELECT * FROM t_item;Execute SQL on specified database shards
When using PolarDB-X 1.0, if you encounter an SQL statement that PolarDB-X 1.0 does not support, you can use the custom HINT provided by PolarDB-X 1.0 to execute the SQL statement directly on one or more sharded databases. Additionally, to query data from a specific sharded database or from a sharded table in a known sharded database, you can also use this custom HINT to execute the SQL statement directly on the target sharded database.
You can use a custom HINT to specify a database shard for SQL execution in two ways: using a shard name or using a database sharding key value. The shard name is the unique identifier for a database shard in PolarDB-X 1.0 and can be obtained using the SHOW NODE control instruction.
Syntax
/!TDDL:NODE='node_name'*/node_namespecifies the shard name. This PolarDB-X 1.0 custom HINT sends an SQL statement to the database shard that corresponds tonode_namefor execution./!TDDL:NODE IN ('node_name'[,'node_name1','node_name2'])*/Use the
inkeyword to specify multiple shard names to execute the SQL statement on multiple database shards. The shard names within the parentheses are separated by commas.Note PolarDB-X 1.0When you use this custom HINT, PolarDB-X 1.0 sends the SQL statement directly to the database shard for execution. Therefore, the table name in the SQL statement must be the name of a table that already exists in that database shard./!TDDL:table_name.partition_key=value [and table_name1.partition_key=value1]*/In this PolarDB-X 1.0 custom HINT,
table_nameis the name of a logical table that is a sharded table.partition_keyis the shard key, andvalueis the specified value for the shard key. In this custom HINT, you can use theandkeyword to specify the shard keys of multiple sharded tables. When you use this PolarDB-X 1.0 custom HINT, PolarDB-X 1.0 determines the database shards and tables on which the SQL statement should be executed, and then sends the statement to the corresponding database shards.
Examples
For a PolarDB-X 1.0 database named drds_test, the result of SHOW NODE is as follows:
mysql> SHOW NODE\G
*************************** 1. row ******************
ID: 0
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0000_RDS
MASTER_READ_COUNT: 212
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
*************************** 2. row ******************
ID: 1
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0001_RDS
MASTER_READ_COUNT: 29
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
*************************** 3. row ******************
ID: 2
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0002_RDS
MASTER_READ_COUNT: 29
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
*************************** 4. row ******************
ID: 3
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0003_RDS
MASTER_READ_COUNT: 29
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
*************************** 5. row ******************
ID: 4
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0004_RDS
MASTER_READ_COUNT: 29
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
*************************** 6. row ******************
ID: 5
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0005_RDS
MASTER_READ_COUNT: 29
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
*************************** 7. row ******************
ID: 6
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0006_RDS
MASTER_READ_COUNT: 29
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
*************************** 8. row ******************
ID: 7
NAME: DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0007_RDS
MASTER_READ_COUNT: 29
SLAVE_READ_COUNT: 0
MASTER_READ_PERCENT: 100%
SLAVE_READ_PERCENT: 0%
8 rows in set (0.02 sec)You can see that each database shard has a NAME property, which is the name of the shard. Each shard name uniquely corresponds to a database shard. For example, the shard name DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0003_RDS corresponds to the database shard drds_test_vtla_0003. After you obtain the shard name, you can use a custom HINT in PolarDB-X 1.0 to specify a database shard for SQL statement execution.
- Execute an SQL statement on the 0th database shard:
/!TDDL:NODE='DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0000_RDS'*/SELECT * FROM table_name; - Execute an SQL statement on multiple database shards:
/!TDDL:NODE IN('DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0000_RDS','DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0006_RDS')*/SELECT * FROM table_name;This SQL statement will execute on the
DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0000_RDSandDRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0006_RDSshards. - View the execution plan for a specific database shard:
/!TDDL:NODE='DRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0000_RDS'*/EXPLAIN SELECT * FROM table_name;This SQL statement displays the execution plan for the
SELECTstatement on the shardDRDS_TEST_1473471355140LRPRDRDS_TEST_VTLA_0000_RDS. - Execute an SQL statement on a database shard by specifying a key value:
PolarDB-X 1.0 does not support subqueries in the
SETclause ofUPDATEstatements. BecauseUPDATEstatements in PolarDB-X 1.0 must specify a shard key, you can use a custom PolarDB-X 1.0 HINT to send the statement directly to a database shard for execution.For example, assume there are two logical tables, t1 and t2. Both are sharded across databases and tables. The statements to create them are as follows:
CREATE TABLE `t1` ( `id` bigint(20) NOT NULL, `name` varchar(20) NOT NULL, `val` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`id`) tbpartition by hash(`name`) tbpartitions 3 CREATE TABLE `t2` ( `id` bigint(20) NOT NULL, `name` varchar(20) NOT NULL, `val` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`id`) tbpartition by hash(`name`) tbpartitions 3The statement to be executed is:
UPDATE t1 SET val=(SELECT val FROM t2 WHERE id=1) WHERE id=1;If you execute this statement directly on PolarDB-X 1.0, an "unsupported" error is returned. However, you can add a custom PolarDB-X 1.0 HINT to the statement and then submit it to PolarDB-X 1.0 for execution. The following is a sample SQL statement:
/!TDDL:t1.id=1 and t2.id=1*/UPDATE t1 SET val=(SELECT val FROM t2 WHERE id=1) WHERE id=1;This statement is sent to the shard of
t1where theIDis 1 for execution. You can use the explain command to view the execution plan for this SQL statement:mysql> explain /!TDDL:t1.id=1 and t2.id=1*/UPDATE t1 SET val=(SELECT val FROM t2 WHERE id=1) WHERE id=1\G *************************** 1. row *************************** GROUP_NAME: TEST_DRDS_1485327111630IXLWTEST_DRDS_IGHF_0001_RDS SQL: UPDATE `t1_2` AS `t1` SET `val` = (SELECT val FROM `t2_2` AS `t2` WHERE `id` = 1) WHERE `id` = 1 PARAMS: {} *************************** 2. row *************************** GROUP_NAME: TEST_DRDS_1485327111630IXLWTEST_DRDS_IGHF_0001_RDS SQL: UPDATE `t1_1` AS `t1` SET `val` = (SELECT val FROM `t2_1` AS `t2` WHERE `id` = 1) WHERE `id` = 1 PARAMS: {} *************************** 3. row *************************** GROUP_NAME: TEST_DRDS_1485327111630IXLWTEST_DRDS_IGHF_0001_RDS SQL: UPDATE `t1_0` AS `t1` SET `val` = (SELECT val FROM `t2_0` AS `t2` WHERE `id` = 1) WHERE `id` = 1 PARAMS: {} 3 rows in set (0.00 sec)The result set of the
explaincommand shows that the SQL statement is rewritten into 3 statements and executed on the database shards. You can also specify a table partitioning key value to narrow the SQL execution scope to a single sharded table:mysql> explain /!TDDL:t1.id=1 and t2.id=1 and t1.name='1'*/UPDATE t1 SET val=(SELECT val FROM t2 WHERE id=1) WHERE id=1\G *************************** 1. row *************************** GROUP_NAME: TEST_DRDS_1485327111630IXLWTEST_DRDS_IGHF_0001_RDS SQL: UPDATE `t1_1` AS `t1` SET `val` = (SELECT val FROM `t2_1` AS `t2` WHERE `id` = 1) WHERE `id` = 1 PARAMS: {} 1 row in set (0.00 sec)
Scan all database and table shards
In addition to sending SQL statements to specific database shards, PolarDB-X 1.0 also provides a custom HINT to scan all database and table shards. With this custom HINT, you can send an SQL statement to every database shard for execution. For example, you can use this custom HINT to view all sharded tables on a specific database shard. You can also use it to view the data volume of the sharded tables for a specific logical table in each database shard.
There are two ways to use a custom HINT in PolarDB-X 1.0 to scan all shards. The first way is to send an SQL statement to each database shard for execution. The second way is to send an SQL statement to each database shard to perform an operation on a logical table.
- Send an SQL statement to all database shards for execution:
/!TDDL:SCAN*/ - Perform an operation on a logical table:
/!TDDL:SCAN='table_name'*/In this HINT,
table_nameis the name of a logical table in the PolarDB-X 1.0 database. This custom HINT is provided for sharding. Ensure thattable_namespecifies a sharded table.