# Creating a Global SQL Binding for All Sessions

+ When creating a SQL Binding with global scope, only new sessions with bound SQL statements will be affected.
  
```
tidb> CREATE ~!@GLOBAL BINDING@!~ FOR
   -> SELECT * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no
   -> USING
   -> SELECT /*+ HASH_JOIN(employees, salaries) */ * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no;
Query OK, 0 rows affected (0.01 sec)
```
------------------------------------------------------------------------------
It is important to note that the scope of execution plan binding is limited. The binding we just performed only affects the current session. To apply it across the entire system, we need to specify the GLOBAL scope as follows:

```
tidb> EXPLAIN SELECT * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no;
+-------------------------+------------+-----------+-----------------+--------------------------------------------------------------------------+
| id                      | estRows    | task      | access object   | operator info                                                            |
+-------------------------+------------+-----------+-----------------+--------------------------------------------------------------------------+
| MergeJoin_8             | 2825288.58 | root      |                 | inner join, left key:db1.employees.emp_no, right key:db1.salaries.emp_no |
| ├─TableReader_32(Build) | 2844047.00 | root      |                 | data:TableFullScan_31                                                    |
| │ └─TableFullScan_31    | 2844047.00 | cop[tikv] | table:salaries  | keep order:true                                                          |
| └─TableReader_30(Probe) | 300024.00  | root      |                 | data:TableFullScan_29                                                    |
|  └─TableFullScan_29     | 300024.00  | cop[tikv] | table:employees | keep order:true                                                          |
+-------------------------+------------+-----------+-----------------+--------------------------------------------------------------------------+
5 rows in set (0.00 sec)

tidb> CREATE GLOBAL BINDING FOR
   -> SELECT * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no
   -> USING
   -> SELECT /*+ HASH_JOIN(employees, salaries) */ * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no;
Query OK, 0 rows affected (0.01 sec)

tidb> EXPLAIN SELECT * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no;
+--------------------------+------------+-----------+-----------------+--------------------------------------------------------------------------+
| id                       | estRows    | task      | access object   | operator info                                                            |
+--------------------------+------------+-----------+-----------------+--------------------------------------------------------------------------+
| MergeJoin_8              | 2825288.58 | root      |                 | inner join, left key:db1.employees.emp_no, right key:db1.salaries.emp_no |
| ├─TableReader_32(Build)  | 2844047.00 | root      |                 | data:TableFullScan_31                                                    |
| │ └─TableFullScan_31     | 2844047.00 | cop[tikv] | table:salaries  | keep order:true                                                          |
| └─TableReader_30(Probe)  | 300024.00  | root      |                 | data:TableFullScan_29                                                    |
|  └─TableFullScan_29      | 300024.00  | cop[tikv] | table:employees | keep order:true                                                          |
+--------------------------+------------+-----------+-----------------+--------------------------------------------------------------------------+
5 rows in set (0.00 sec)

tidb> EXIT;

...
Your MySQL connection id is 40431
Server version: 5.7.25-TiDB-v6.5.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible
...

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

tidb> EXPLAIN SELECT * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no;
+-------------------------+------------+-----------+-----------------+-------------------------------------------------------------------+
| id                      | estRows    | task      | access object   | operator info                                                     |
+-------------------------+------------+-----------+-----------------+-------------------------------------------------------------------+
| HashJoin_27             | 2825288.58 | root      |                 | inner join, equal:[eq(db1.employees.emp_no, db1.salaries.emp_no)] |
| ├─TableReader_29(Build) | 300024.00  | root      |                 | data:TableFullScan_28                                             |
| │ └─TableFullScan_28    | 300024.00  | cop[tikv] | table:employees | keep order:false                                                  |
| └─TableReader_31(Probe) | 2844047.00 | root      |                 | data:TableFullScan_30                                             |
|  └─TableFullScan_30     | 2844047.00 | cop[tikv] | table:salaries  | keep order:false                                                  |
+-------------------------+------------+-----------+-----------------+-------------------------------------------------------------------+
5 rows in set (0.00 sec)
```

As observed, when we use the `CREATE GLOBAL BINDING FOR` command to bind an execution plan, it does not immediately take effect in the current session or any other active sessions. However, upon reconnecting, any new session will utilize our bound execution plan for the statement `SELECT * FROM employees, salaries WHERE employees.emp_no = salaries.emp_no`. This indicates that specifying the global scope means that only newly created sessions with bound SQL statements will be affected by execution plan bindings.

For detailed syntax and considerations regarding execution plan binding, please refer to the official documentation: https://docs.pingcap.com/tidb/stable/sql-plan-management#sql-binding 

With SQL binding, we can modify poorly performing statements in a production environment that cannot be directly altered in the SQL code.