Scan all or some sharded databases and tables

Updated at:

This topic describes the HINT syntax and provides examples for scanning all or some sharded databases and tables.

In addition to sending an SQL statement to one or more sharded databases for execution, PolarDB-X provides the SCAN HINT to scan all or some sharded databases and tables. You can use the SCAN HINT to send an SQL statement to every sharded database for execution. For example, you can view all sharded tables in a specific sharded database or check the data volume of each physical table for a logical table.

The SCAN HINT provides four ways to execute an SQL statement:
  1. Execute on all sharded tables in all sharded databases.
  2. Execute on all sharded tables in specified sharded databases.
  3. Execute on specified sharded databases and tables, and compute physical table names based on a condition.
  4. Execute on specified sharded databases and tables, and explicitly specify physical table names.

The SCAN HINT supports DML, DDL, and some DAL statements.

Syntax

# SCAN HINT

# Send the SQL statement to all sharded tables in all sharded databases for execution.
SCAN()                               

# Send the SQL statement to all sharded tables in the specified sharded databases for execution.
SCAN(NODE="node_list")               # Specify sharded databases.

# Send the SQL statement to the specified sharded databases and tables for execution, and compute physical table names based on a condition.
SCAN(
  [TABLE=]"table_name_list"          # Logical table name.
  , CONDITION="condition_string"     # Use the content in TABLE and CONDITION to compute the names of physical databases and tables.
  [, NODE="node_list"] )             # Filter the results computed by CONDITION and retain only the specified physical databases.

# Send the SQL statement to the specified sharded databases and tables for execution, and explicitly specify physical table names.
SCAN(
  [TABLE=]"table_name_list"          # Logical table name.
  , REAL_TABLE=("table_name_list")   # Physical table name. The same physical table names are used for all physical databases.
  [, NODE="node_list"] )             # Filter the results computed by CONDITION and retain only the specified physical databases.


# List of physical or logical table names.
table_name_list: 
    table_name [, table_name]...

# List of physical databases. Supports the ordinal numbers of GROUP_KEY and GROUP. Obtain the numbers using the `SHOW NODE` statement.
node_list: 
    {group_key | group_index} [, {group_key | group_index}]...
    
# Supports SQL WHERE syntax. Set a condition for each table, such as: t1.id = 2 and t2.id = 2
condition_string: 
    where_condition

            

Notes

  • PolarDB-X custom HINTs support two formats: /*+TDDL:hint_command*/ and /!+TDDL:hint_command*/.
  • If you use the /*+TDDL:hint_command*/ format, you must add the -c parameter to the logon command when you execute an SQL statement with a custom HINT from the official MySQL command-line client. This is because PolarDB-X custom HINTs use the MySQL comment format. Without the -c parameter, the client removes the comment before sending the statement to the server, which causes the PolarDB-X custom HINT to fail. For more information, see the official MySQL client command options.

Examples

  • Execute on all sharded tables in all sharded databases:
    SELECT /*+TDDL:scan()*/ COUNT(1) FROM t1      

    The SQL statement is sent to all physical tables of t1 for execution. The result sets are then merged and returned.

  • Execute on all sharded tables in specified sharded databases:
    SELECT /*+TDDL:scan(node='0,1,2')*/ COUNT(1) FROM t1          

    First, all physical tables of t1 in the 0000, 0001, and 0002 sharded databases are computed. Then, the SQL statement is sent for execution. The result sets are merged and returned.

  • Execute on specified sharded tables based on a condition:
    SELECT /*+TDDL:scan('t1', condition='t1.id = 2')*/ COUNT(1) FROM t1           

    First, all physical tables of the logical table t1 that meet the condition are computed. Then, the SQL statement is sent for execution. The result sets are merged and returned.

  • Execute on specified sharded tables based on a condition, with a JOIN:
    SELECT /*+TDDL:scan('t1, t2', condition='t1.id = 2 and t2.id = 2')*/ * FROM t1 a JOIN t2 b ON a.id = b.id WHERE b.name = "test"
                

    First, all physical tables of the logical tables t1 and t2 that meet the condition are computed. Then, the SQL statement is sent for execution. The result sets are merged and returned. Note: To use this custom comment, ensure that the two tables have the same number of sharded databases and sharded tables. Otherwise, PolarDB-X computes different sharded databases for the two key values, which causes an error.

  • Execute on specified sharded databases and tables, and explicitly specify physical table names:
    SELECT /*+TDDL:scan('t1', real_table=("t1_00", "t1_01"))*/ COUNT(1) FROM t1
                

    The SQL statement is sent to the t1_00 and t1_01 sharded tables in all sharded databases. The result sets are then merged and returned.

  • Execute on specified sharded databases and tables, explicitly specify physical table names, with a JOIN:
    SELECT /*+TDDL:scan('t1, t2', real_table=("t1_00,t2_00", "t1_01,t2_01"))*/ * FROM t1 a JOIN t2 b ON a.id = b.id WHERE b.name = "test";
                

    The SQL statement is sent to the t1_00, t2_00, t1_01, and t2_01 sharded tables in all sharded databases. The result sets are then merged and returned.