Skip to content

Commit

Permalink
sql statements: add create binding, drop binding and show bindings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lzmhhh123 authored May 26, 2020
1 parent 2e1c60a commit 57078dd
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
3 changes: 3 additions & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
- [`ANALYZE TABLE`](/sql-statements/sql-statement-analyze-table.md)
- [`BEGIN`](/sql-statements/sql-statement-begin.md)
- [`COMMIT`](/sql-statements/sql-statement-commit.md)
- [`CREATE BINDING`](/sql-statements/sql-statement-create-binding.md)
- [`CREATE DATABASE`](/sql-statements/sql-statement-create-database.md)
- [`CREATE INDEX`](/sql-statements/sql-statement-create-index.md)
- [`CREATE SEQUENCE`](/sql-statements/sql-statement-create-sequence.md)
Expand All @@ -261,6 +262,7 @@
- [`DESC`](/sql-statements/sql-statement-desc.md)
- [`DESCRIBE`](/sql-statements/sql-statement-describe.md)
- [`DO`](/sql-statements/sql-statement-do.md)
- [`DROP BINDING`](/sql-statements/sql-statement-drop-binding.md)
- [`DROP COLUMN`](/sql-statements/sql-statement-drop-column.md)
- [`DROP DATABASE`](/sql-statements/sql-statement-drop-database.md)
- [`DROP INDEX`](/sql-statements/sql-statement-drop-index.md)
Expand Down Expand Up @@ -293,6 +295,7 @@
- [`SET PASSWORD`](/sql-statements/sql-statement-set-password.md)
- [`SET TRANSACTION`](/sql-statements/sql-statement-set-transaction.md)
- [`SET [GLOBAL|SESSION] <variable>`](/sql-statements/sql-statement-set-variable.md)
- [`SHOW BINDINGS`](/sql-statements/sql-statement-show-bindings.md)
- [`SHOW BUILTINS`](/sql-statements/sql-statement-show-builtins.md)
- [`SHOW CHARACTER SET`](/sql-statements/sql-statement-show-character-set.md)
- [`SHOW COLLATION`](/sql-statements/sql-statement-show-collation.md)
Expand Down
Binary file modified media/sqlgram/CreateBindingStmt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified media/sqlgram/DropBindingStmt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified media/sqlgram/GlobalScope.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions sql-statements/sql-statement-create-binding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: CREATE BINDING
summary: TiDB 数据库中 CREATE BINDING 的使用概况。
category: reference
---

# CREATE BINDING

`CREATE BINDING` 语句用于在 TiDB 上创建新 SQL BIND。

## 语法图

**CreateBindingStmt:**

![CreateBindingStmt](/media/sqlgram/CreateBindingStmt.png)

**GlobalScope:**

![GlobalScope](/media/sqlgram/GlobalScope.png)

**SelectStmt**

![SelectStmt](/media/sqlgram/SelectStmt.png)

****

## 语法说明

{{< copyable "sql" >}}

```sql
CREATE [GLOBAL | SESSION] BINDING FOR SelectStmt USING SelectStmt;
```

该语句可以在 GLOBAL 或者 SESSION 作用域内为 SQL 绑定执行计划。在不指定作用域时,隐式作用域为 SESSION。

被绑定的 SQL 会被参数化后存储到系统表中。在处理 SQL 查询时,只要参数化后的 SQL 和系统表中某个被绑定的 SQL 语句一致,并且系统变量 `tidb_use_plan_baselines` 的值为 `on`(其默认值为 `on`),即可使用相应的优化器 Hint。如果存在多个可匹配的执行计划,优化器会从中选择代价最小的一个进行绑定。

## 另请参阅

* [DROP BINDING](/sql-statements/sql-statement-drop-binding.md)
* [SHOW BINDINGS](/sql-statements/sql-statement-show-bindings.md)
38 changes: 38 additions & 0 deletions sql-statements/sql-statement-drop-binding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: DROP BINDING
summary: TiDB 数据库中 DROP BINDING 的使用概况。
category: reference
---

# DROP BINDING

`DROP BINDING` 语句用于逻辑删除指定的 SQL BIND。

## 语法图

**DropBindingStmt:**

![DropBindingStmt](/media/sqlgram/DropBindingStmt.png)

**GlobalScope:**

![GlobalScope](/media/sqlgram/GlobalScope.png)

**SelectStmt**

![SelectStmt](/media/sqlgram/SelectStmt.png)

## 语法说明

{{< copyable "sql" >}}

```sql
DROP [GLOBAL | SESSION] BINDING FOR SelectStmt;
```

该语句可以在 GLOBAL 或者 SESSION 作用域内删除指定的执行计划绑定,在不指定作用域时默认作用域为 SESSION。

## 另请参阅

* [CREATE BINDING](/sql-statements/sql-statement-create-binding.md)
* [SHOW BINDINGS](/sql-statements/sql-statement-show-bindings.md)
53 changes: 53 additions & 0 deletions sql-statements/sql-statement-show-bindings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: SHOW BINDINGS
summary: TiDB 数据库中 SHOW BINDINGS 的使用概况。
category: reference
---

# SHOW BINDINGS

`SHOW BINDINGS` 语句用于显示所有创建过的 SQL BIND 的相关信息。

## 语法图

**ShowStmt:**

![ShowStmt](/media/sqlgram/ShowStmt.png)

**ShowTargetFilterable:**

![ShowTargetFilterable](/media/sqlgram/ShowTargetFilterable.png)

**GlobalScope:**

![GlobalScope](/media/sqlgram/GlobalScope.png)

**ShowLikeOrWhereOpt**

![ShowLikeOrWhereOpt](/media/sqlgram/ShowLikeOrWhereOpt.png)

## 语法说明

{{< copyable "sql" >}}

```sql
SHOW [GLOBAL | SESSION] BINDINGS [ShowLikeOrWhereOpt];
```

该语句会输出 GLOBAL 或者 SESSION 作用域内的执行计划绑定,在不指定作用域时默认作用域为 SESSION。目前 `SHOW BINDINGS` 会输出 8 列,具体如下:

| 列名 | 说明 |
| -------- | ------------- |
| original_sql | 参数化后的原始 SQL |
| bind_sql | 带 Hint 的绑定 SQL |
| default_db | 默认数据库名 |
| status | 状态,包括 using(正在使用)、deleted(已删除)、 invalid(无效)、rejected(演进时被拒绝)和 pending verify(等待演进验证) |
| create_time | 创建时间 |
| update_time | 更新时间 |
| charset | 字符集 |
| collation | 排序规则 |

## 另请参阅

* [CREATE BINDING](/sql-statements/sql-statement-create-binding.md)
* [DROP BINDING](/sql-statements/sql-statement-drop-binding.md)

0 comments on commit 57078dd

Please sign in to comment.