# Control Execution Plan and Best Practices in SQL
 
### Overview
+ In this practice, you use the SQL hints and SPM to stabilize the execution plan of the query statement that is currently running.

### Notes
+ The following experiments are designed to run on TiDB v7.5. Please install and deploy the TiDB v7.5 version database before the exercises. If you use an earlier version of TiDB database, the results may not be consistent with the guide.
+ Many of the steps require that you type commands in a terminal window. You might find it convenient to launch a terminal window and keep it open for the duration of the exercises.
+ Your environment may be different with these exercises, including but not limited to `IP`, `port`, `username`, `password` and `directory`.
+ The `$` in your terminal window is an OS prompt.
+ The "`tidb>`" or "`tidb>`" in your terminal window is SQL command prompt. 
+ In these exercises, we need 10 machines connected in the network. The `Role` and `IP Address` (represented by environment variables) are as follows:
    | `Node Role`        | `IP Address`                                              |
    | :----------------- | :-------------------------------------------------------- |
    | `Monitor 1`        | `${HOST_MONITOR1_PRIVATE_IP}, ${HOST_MONITOR1_PUBLIC_IP}` |
    | `Control Machine`  | `${HOST_CM_PRIVATE_IP}, ${HOST_CM_PUBLIC_IP}`             |
    | `TiDB server 1`    | `${HOST_DB1_PRIVATE_IP}, ${HOST_DB1_PUBLIC_IP}`           |
    | `TiDB server 2`    | `${HOST_DB2_PRIVATE_IP}, ${HOST_DB2_PUBLIC_IP}`           |
    | `TiKV server 1`    | `${HOST_KV1_PRIVATE_IP}, ${HOST_KV1_PUBLIC_IP}`           |
    | `TiKV server 2`    | `${HOST_KV2_PRIVATE_IP}, ${HOST_KV2_PUBLIC_IP}`           |
    | `TiKV server 3`    | `${HOST_KV3_PRIVATE_IP}, ${HOST_KV3_PUBLIC_IP}`           |
    | `PD server 1`      | `${HOST_PD1_PRIVATE_IP}, ${HOST_PD1_PUBLIC_IP}`           |
    | `PD server 2`      | `${HOST_PD2_PRIVATE_IP}, ${HOST_PD2_PUBLIC_IP}`           |
    | `PD server 3`      | `${HOST_PD3_PRIVATE_IP}, ${HOST_PD3_PUBLIC_IP}`           |
    | `TiFlash server 1` | `${HOST_TIFLASH1_PRIVATE_IP}, ${HOST_TIFLASH1_PUBLIC_IP}` |
    | `Alertmanager`     | `${HOST_MONITOR1_PRIVATE_IP}, ${HOST_MONITOR1_PUBLIC_IP}` |
    | `Grafana`          | `${HOST_MONITOR1_PRIVATE_IP}, ${HOST_MONITOR1_PUBLIC_IP}` |
    | `Prometheus`       | `${HOST_MONITOR1_PRIVATE_IP}, ${HOST_MONITOR1_PUBLIC_IP}` |

### Assumptions
+ You are given the user credentials (for example: the private key) of the exercise environment. 
+ You are given access to 10 machines that have been set up.
+ Your environment has TiDB cluster deployed and running.

## Exercise 1: Indexing for Query Performance
+ In this exercise, you use the SQL hints and SPM to stabilize the execution plan of the query statement that is currently running.

### Duration
+ This exercise should take you approximately 20 minutes to complete.

### Tasks 
1. Open a Linux terminal window and start an interactive TiDB session.
2. Make `test` the current database.
3. Execute the following statement to create table t1 and t2 and their indexes for your exercise:
    ```sql
    CREATE TABLE t1(a int, b int, c varchar(10), d varchar(20));
    CREATE TABLE t2(a int, b int, c varchar(10), d varchar(20));
    CREATE INDEX t1_ind_1 ON t1(a);
    CREATE INDEX t1_ind_2 ON t1(a,b,c);
    CREATE INDEX t2_ind_1 ON t2(a);
    CREATE INDEX t2_ind_2 ON t2(a,b,c);
    ```
4. Exit the TiDB Session to Linux terminal window and insert date into t1 and t2.
    ```
    for i in `seq 10000`; do mysql -h${HOST_DB1_PRIVATE_IP} -P4000 -uroot -p<YOUR PASSWORD> -e "INSERT INTO test.t1 VALUES($i, FLOOR(RAND()*10000000), SUBSTRING(MD5(RAND()), 1, 5), SUBSTRING(MD5(RAND()), 1, 10))"; done;
    for i in `seq 10000`; do mysql -h${HOST_DB1_PRIVATE_IP} -P4000 -uroot -p<YOUR PASSWORD> -e "INSERT INTO test.t2 VALUES($i, FLOOR(RAND()*10000000), SUBSTRING(MD5(RAND()), 1, 5), SUBSTRING(MD5(RAND()), 1, 10))"; done;
    ```
5. Start an interactive TiDB session again.
6. Make `test` the current database.
7. Collect the statistic of table `t1` and `t2`;
8. Execute the following statement to view the EXPLAIN output for a query against the `t1` and `t2` table:
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
9. You would like to avoid using MergeJoin and use IndexJoin instead, so you use SQL Hint to guide the use of index `t2_ind_1` on table `t2` instead of `t2_ind_2`. 
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 USE INDEX(t2_ind_1) WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
10. Use SPM to let all SQL to use index `t2_ind_1` and use HashJoin as the join type for all queries in step 8 without changing the SQL statements in the running program.
11. Re-issue the EXPLAIN statement from step 8. Explain the difference in the output. 
12. Open another new Linux terminal window and start an interactive TiDB session.
13. In the new Linux terminal, make `test` the current database.
14. In the new Linux terminal, re-issue the EXPLAIN statement from step 8. Explain the difference in the output.
15. Check the global bindings in any linux terminal.
16. Drop the global bindings in any linux terminal.
17. In any Linux terminal, re-issue the EXPLAIN statement from step 8. Explain the difference in the output.
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
18. Clean up the table t1 and t2.
19. Close the new terminal window.
20. Leave the original terminal window with the TiDB session open for the next practice.

### Solution
1. Login as user `ec2-user` on `Control Machine` (`${HOST_MONITOR1_PUBLIC_IP}`). Enter the following command at the terminal prompt and receive the results shown: 
    ```
    $ mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root -p<your_password>
    mysql> prompt tidb>
    PROMPT set to 'tidb>'
    ```

2. Make `test` the current database.
   Enter the following statement at the TiDB prompt and receive the results shown:
   ```sql
   USE test;
   ```
   ```
   tidb>USE test;
   Database changed
   ```

3. Execute the following statement to create table t1 and t2 and their indexes for your exercise. Enter the following command at the terminal prompt and receive the results shown: 
    ```sql
    CREATE TABLE t1(a int, b int, c varchar(10), d varchar(20));
    CREATE TABLE t2(a int, b int, c varchar(10), d varchar(20));
    CREATE INDEX t1_ind_1 ON t1(a);
    CREATE INDEX t1_ind_2 ON t1(a,b,c);
    CREATE INDEX t2_ind_1 ON t2(a);
    CREATE INDEX t2_ind_2 ON t2(a,b,c);
    ```
    ```
    tidb>CREATE TABLE t1(a int, b int, c varchar(10), d varchar(20));
    Query OK, 0 rows affected (0.54 sec)

    tidb>CREATE TABLE t2(a int, b int, c varchar(10), d varchar(20));
    Query OK, 0 rows affected (0.52 sec)

    tidb>CREATE INDEX t1_ind_1 ON t1(a);
    Query OK, 0 rows affected (1.03 sec)

    tidb>CREATE INDEX t1_ind_2 ON t1(a,b,c);
    Query OK, 0 rows affected (1.03 sec)

    tidb>CREATE INDEX t2_ind_1 ON t2(a);
    Query OK, 0 rows affected (1.02 sec)

    tidb>CREATE INDEX t2_ind_2 ON t2(a,b,c);
    Query OK, 0 rows affected (1.03 sec)
    ```

4. Exit the TiDB Session to Linux terminal window and insert date into t1 and t2. Enter the following command at the terminal prompt and receive the results shown: 
    ```
    for i in `seq 10000`; do mysql -h${HOST_DB1_PRIVATE_IP} -P4000 -uroot -p<YOUR PASSWORD> -e "INSERT INTO test.t1 VALUES($i, FLOOR(RAND()*10000000), SUBSTRING(MD5(RAND()), 1, 5), SUBSTRING(MD5(RAND()), 1, 10))"; done;
    for i in `seq 10000`; do mysql -h${HOST_DB1_PRIVATE_IP} -P4000 -uroot -p<YOUR PASSWORD> -e "INSERT INTO test.t2 VALUES($i, FLOOR(RAND()*10000000), SUBSTRING(MD5(RAND()), 1, 5), SUBSTRING(MD5(RAND()), 1, 10))"; done;
    ```
    ```
    $for i in `seq 10000`; do mysql -h${HOST_DB1_PRIVATE_IP} -P4000 -uroot -p<YOUR PASSWORD> -e "INSERT INTO test.t1 VALUES($i, FLOOR(RAND()*10000000), SUBSTRING(MD5(RAND()), 1, 5), SUBSTRING(MD5(RAND()), 1, 10))"; done;
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ...
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    $for i in `seq 10000`; do mysql -h${HOST_DB1_PRIVATE_IP} -P4000 -uroot -p<YOUR PASSWORD> -e "INSERT INTO test.t2 VALUES($i, FLOOR(RAND()*10000000), SUBSTRING(MD5(RAND()), 1, 5), SUBSTRING(MD5(RAND()), 1, 10))"; done;
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ...
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ```
5. Start an interactive TiDB session again. Enter the following command at the terminal prompt and receive the results shown: 
    ```
    $ mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root -p<your_password>
    mysql> prompt tidb>
    PROMPT set to 'tidb>'
    ```

6. Make `test` the current database.
Enter the following statement at the TiDB prompt and receive the results shown:
   ```sql
   USE test;
   ```
   ```
   tidb>USE test;
   Database changed
   ```

7. Collect the statistic of table `t1` and `t2`. Enter the following statement at the TiDB prompt and receive the results shown:
    ```sql
    ANALYZE TABLE t1;
    ANALYZE TABLE t2;
    ```
    ```
    tidb> ANALYZE TABLE t1;
    Query OK, 0 rows affected, 1 warning (1.40 sec)

    tidb> ANALYZE TABLE t2;
    Query OK, 0 rows affected, 1 warning (1.41 sec)
    ```

8. Execute the following statement to view the EXPLAIN output for a query against the `t1` and `t2` table. Enter the following statement at the TiDB prompt and receive the results shown:
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
    ```
    tidb>EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    +--------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------+
    | id                             | estRows  | task      | access object                     | operator info                                                                            |
    +--------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------+
    | Sort_8                         | 10000.00 | root      |                                   | test.t1.b, test.t2.b                                                                     |
    | └─MergeJoin_11                 | 10000.00 | root      |                                   | inner join, left key:test.t1.a, right key:test.t2.a, other cond:gt(test.t1.b, test.t2.b) |
    |   ├─IndexReader_42(Build)      | 10000.00 | root      |                                   | index:Selection_41                                                                       |
    |   │ └─Selection_41             | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.b))                                                                   |
    |   │   └─IndexFullScan_40       | 10000.00 | cop[tikv] | table:t2, index:t2_ind_2(a, b, c) | keep order:true                                                                          |
    |   └─IndexReader_39(Probe)      | 10000.00 | root      |                                   | index:Selection_38                                                                       |
    |     └─Selection_38             | 10000.00 | cop[tikv] |                                   | not(isnull(test.t1.b))                                                                   |
    |       └─IndexFullScan_37       | 10000.00 | cop[tikv] | table:t1, index:t1_ind_2(a, b, c) | keep order:true                                                                          |
    +--------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------+
    8 rows in set (0.02 sec)
    ```

9. You would like to avoid using MergeJoin and use IndexJoin instead, so you use SQL Hint to guide the use of index `t2_ind_1` on table `t2` instead of `t2_ind_2`. Enter the following statement at the TiDB prompt and receive the results shown:
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 USE INDEX(t2_ind_1) WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
    ```
    tidb>EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 USE INDEX(t2_ind_1) WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id                              | estRows  | task      | access object                     | operator info                                                                                                                                        |
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Sort_8                          | 10000.00 | root      |                                   | test.t1.b, test.t2.b                                                                                                                                 |
    | └─IndexJoin_18                  | 10000.00 | root      |                                   | inner join, inner:IndexLookUp_17, outer key:test.t1.a, inner key:test.t2.a, equal cond:eq(test.t1.a, test.t2.a), other cond:gt(test.t1.b, test.t2.b) |
    |   ├─IndexReader_52(Build)       | 10000.00 | root      |                                   | index:Selection_51                                                                                                                                   |
    |   │ └─Selection_51              | 10000.00 | cop[tikv] |                                   | not(isnull(test.t1.b))                                                                                                                               |
    |   │   └─IndexFullScan_50        | 10000.00 | cop[tikv] | table:t1, index:t1_ind_2(a, b, c) | keep order:false                                                                                                                                     |
    |   └─IndexLookUp_17(Probe)       | 10000.00 | root      |                                   |                                                                                                                                                      |
    |     ├─Selection_15(Build)       | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.a))                                                                                                                               |
    |     │ └─IndexRangeScan_13       | 10000.00 | cop[tikv] | table:t2, index:t2_ind_1(a)       | range: decided by [eq(test.t2.a, test.t1.a)], keep order:false                                                                                       |
    |     └─Selection_16(Probe)       | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.b))                                                                                                                               |
    |       └─TableRowIDScan_14       | 10000.00 | cop[tikv] | table:t2                          | keep order:false                                                                                                                                     |
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    10 rows in set (0.00 sec)
    ```

10. Use SPM to let all SQL to use index `t2_ind_1` and use HashJoin as the join type for all queries in step 8 without changing the SQL statements in the running program. Enter the following statement at the TiDB prompt and receive the results shown:
    ```sql
    CREATE GLOBAL BINDING FOR 
    SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b
    USING
    SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 USE INDEX(t2_ind_1) WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
    ```
    tidb>CREATE GLOBAL BINDING FOR 
        -> SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b
        -> USING
        -> SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 USE INDEX(t2_ind_1) WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    Query OK, 0 rows affected (0.01 sec)
    ```

11. Re-issue the EXPLAIN statement from step 8. Explain the difference in the output. Enter the following statement at the TiDB prompt and receive the results shown: 
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
    ```
    tidb>EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id                              | estRows  | task      | access object                     | operator info                                                                                                                                        |
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Sort_8                          | 10000.00 | root      |                                   | test.t1.b, test.t2.b                                                                                                                                 |
    | └─IndexJoin_18                  | 10000.00 | root      |                                   | inner join, inner:IndexLookUp_17, outer key:test.t1.a, inner key:test.t2.a, equal cond:eq(test.t1.a, test.t2.a), other cond:gt(test.t1.b, test.t2.b) |
    |   ├─IndexReader_52(Build)       | 10000.00 | root      |                                   | index:Selection_51                                                                                                                                   |
    |   │ └─Selection_51              | 10000.00 | cop[tikv] |                                   | not(isnull(test.t1.b))                                                                                                                               |
    |   │   └─IndexFullScan_50        | 10000.00 | cop[tikv] | table:t1, index:t1_ind_2(a, b, c) | keep order:false                                                                                                                                     |
    |   └─IndexLookUp_17(Probe)       | 10000.00 | root      |                                   |                                                                                                                                                      |
    |     ├─Selection_15(Build)       | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.a))                                                                                                                               |
    |     │ └─IndexRangeScan_13       | 10000.00 | cop[tikv] | table:t2, index:t2_ind_1(a)       | range: decided by [eq(test.t2.a, test.t1.a)], keep order:false                                                                                       |
    |     └─Selection_16(Probe)       | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.b))                                                                                                                               |
    |       └─TableRowIDScan_14       | 10000.00 | cop[tikv] | table:t2                          | keep order:false                                                                                                                                     |
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    10 rows in set (0.00 sec) 
    ```

12. Open another new Linux terminal window and start an interactive TiDB session. Enter the following statement at the TiDB prompt and receive the results shown: 
    ```
    $ mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root -p<your_password>
    mysql> prompt tidb>
    PROMPT set to 'tidb>'
    ```

13. Make `test` the current database.
   Enter the following statement at the TiDB prompt and receive the results shown:
   ```sql
   USE test;
   ```
   ```
   tidb>USE test;
   Database changed
   ```

14. In the new Linux terminal, re-issue the EXPLAIN statement from step 8. Explain the difference in the output. Enter the following statement at the TiDB prompt and receive the results shown: 
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
    ```
    tidb>EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id                              | estRows  | task      | access object                     | operator info                                                                                                                                        |
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Sort_8                          | 10000.00 | root      |                                   | test.t1.b, test.t2.b                                                                                                                                 |
    | └─IndexJoin_18                  | 10000.00 | root      |                                   | inner join, inner:IndexLookUp_17, outer key:test.t1.a, inner key:test.t2.a, equal cond:eq(test.t1.a, test.t2.a), other cond:gt(test.t1.b, test.t2.b) |
    |   ├─IndexReader_52(Build)       | 10000.00 | root      |                                   | index:Selection_51                                                                                                                                   |
    |   │ └─Selection_51              | 10000.00 | cop[tikv] |                                   | not(isnull(test.t1.b))                                                                                                                               |
    |   │   └─IndexFullScan_50        | 10000.00 | cop[tikv] | table:t1, index:t1_ind_2(a, b, c) | keep order:false                                                                                                                                     |
    |   └─IndexLookUp_17(Probe)       | 10000.00 | root      |                                   |                                                                                                                                                      |
    |     ├─Selection_15(Build)       | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.a))                                                                                                                               |
    |     │ └─IndexRangeScan_13       | 10000.00 | cop[tikv] | table:t2, index:t2_ind_1(a)       | range: decided by [eq(test.t2.a, test.t1.a)], keep order:false                                                                                       |
    |     └─Selection_16(Probe)       | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.b))                                                                                                                               |
    |       └─TableRowIDScan_14       | 10000.00 | cop[tikv] | table:t2                          | keep order:false                                                                                                                                     |
    +---------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
    10 rows in set (0.00 sec)
    ```

15. Check the global bindings in any linux terminal. Enter the following statement at the TiDB prompt and receive the results shown: 
    ```sql
    SHOW GLOBAL BINDINGS WHERE ORIGINAL_SQL LIKE '%t1%t2%'\G;
    ```
    ```
    tidb>SHOW GLOBAL BINDINGS WHERE ORIGINAL_SQL LIKE '%t1%t2%'\G;
    *************************** 1. row ***************************
    Original_sql: select `t1` . `a` , `t1` . `b` , `t1` . `c` , `t2` . `b` from ( `test` . `t1` ) join `test` . `t2` where `t1` . `a` = `t2` . `a` and `t1` . `b` > `t2` . `b` order by `t1` . `b` , `t2` . `b`
        Bind_sql: SELECT `t1`.`a`,`t1`.`b`,`t1`.`c`,`t2`.`b` FROM (`test`.`t1`) JOIN `test`.`t2` USE INDEX (`t2_ind_1`) WHERE `t1`.`a` = `t2`.`a` AND `t1`.`b` > `t2`.`b` ORDER BY `t1`.`b`,`t2`.`b`
    Default_db: test
        Status: enabled
    Create_time: 2023-12-04 12:22:50.927
    Update_time: 2023-12-04 12:22:50.927
        Charset: utf8
    Collation: utf8_general_ci
        Source: manual
    Sql_digest: fc14be07f408211ab8bcba115f45f2c602250e16e3d540960fceb7238c4e9d87
    Plan_digest: 
    1 row in set (0.00 sec)
    ```

16. Drop the global bindings in any linux terminal. Enter the following statement at the TiDB prompt and receive the results shown:
    ```sql
    DROP GLOBAL BINDING FOR SQL DIGEST 'fc14be07f408211ab8bcba115f45f2c602250e16e3d540960fceb7238c4e9d87';
    ```
    ```
    tidb>DROP GLOBAL BINDING FOR SQL DIGEST 'fc14be07f408211ab8bcba115f45f2c602250e16e3d540960fceb7238c4e9d87';
    Query OK, 1 row affected (0.01 sec)
    ```

17. In any Linux terminal, re-issue the EXPLAIN statement from step 8. Explain the difference in the output. Enter the following statement at the TiDB prompt and receive the results shown:
    ```sql
    EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    ```
    ```
    tidb>EXPLAIN SELECT t1.a, t1.b, t1.c, t2.b FROM t1, t2 WHERE t1.a = t2.a AND t1.b > t2.b ORDER BY t1.b, t2.b;
    +--------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------+
    | id                             | estRows  | task      | access object                     | operator info                                                                            |
    +--------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------+
    | Sort_8                         | 10000.00 | root      |                                   | test.t1.b, test.t2.b                                                                     |
    | └─MergeJoin_11                 | 10000.00 | root      |                                   | inner join, left key:test.t1.a, right key:test.t2.a, other cond:gt(test.t1.b, test.t2.b) |
    |   ├─IndexReader_42(Build)      | 10000.00 | root      |                                   | index:Selection_41                                                                       |
    |   │ └─Selection_41             | 10000.00 | cop[tikv] |                                   | not(isnull(test.t2.b))                                                                   |
    |   │   └─IndexFullScan_40       | 10000.00 | cop[tikv] | table:t2, index:t2_ind_2(a, b, c) | keep order:true                                                                          |
    |   └─IndexReader_39(Probe)      | 10000.00 | root      |                                   | index:Selection_38                                                                       |
    |     └─Selection_38             | 10000.00 | cop[tikv] |                                   | not(isnull(test.t1.b))                                                                   |
    |       └─IndexFullScan_37       | 10000.00 | cop[tikv] | table:t1, index:t1_ind_2(a, b, c) | keep order:true                                                                          |
    +--------------------------------+----------+-----------+-----------------------------------+------------------------------------------------------------------------------------------+
    8 rows in set (0.00 sec)
    ```

18. Clean up the table t1 and t2. Enter the following statement at the TiDB prompt and receive the results shown:
    ```sql
    DROP TABLE t1,t2;
    ```
    ```
    tidb>DROP TABLE t1,t2;
    Query OK, 0 rows affected (1.06 sec)
    ``` 

19. Close the new terminal window.
    ```
    tidb>EXIT
    Bye
    $ exit
    logout
    ```

20. Leave the original terminal window with the TiDB session open for the next practice.

## End of Exercises