Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update doc for SPM #6790

Merged
merged 21 commits into from
Aug 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions sql-plan-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ explain select * from t1,t2 where t1.id = t2.id;

在这里 SESSION 作用域内被删除掉的绑定会屏蔽 GLOBAL 作用域内相应的绑定,优化器不会为 `select` 语句添加 `sm_join(t1, t2)` hint,`explain` 给出的执行计划中最上层节点并不被 hint 固定为 MergeJoin,而是由优化器经过代价估算后自主进行选择。

> **注意:**
>
> 执行 `DROP GLOBAL BINDING` 会删除当前 tidb-server 实例缓存中的绑定,并将系统表中对应行的状态修改为 'deleted'。该语句不会直接删除系统表中的记录,因为其他 tidb-server 实例需要读取系统表中的 'deleted' 状态来删除其缓存中对应的绑定。对于这些系统表中状态为 'deleted' 的记录,后台线程每隔 100 个 `bind-info-lease`(默认值为 `3s`,合计 `300s`)会触发一次对 `update_time` 在 10 个 `bind-info-lease` 以前的绑定(确保所有 tidb-server 实例已经读取过这个 'deleted' 状态并更新完缓存)的回收清除操作。

### 查看绑定

{{< copyable "sql" >}}
Expand All @@ -173,7 +177,7 @@ explain select * from t1,t2 where t1.id = t2.id;
SHOW [GLOBAL | SESSION] BINDINGS [ShowLikeOrWhere];
```

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

| 列名 | 说明 |
| -------- | ------------- |
Expand All @@ -187,6 +191,32 @@ SHOW [GLOBAL | SESSION] BINDINGS [ShowLikeOrWhere];
| collation | 排序规则 |
| source | 创建方式,包括 manual (由 `create [global] binding` 生成)、capture(由 tidb 自动创建生成)和 evolve (由 tidb 自动演进生成) |

### 排查绑定

{{< copyable "sql" >}}

```sql
SELECT [SESSION] @@last_plan_from_binding;
```

该语句使用系统变量 [`last_plan_from_binding`](/system-variables.md#last_plan_from_binding-从-v40-版本开始引入) 显示上一条执行的语句所使用的执行计划是否来自 binding 的执行计划。

另外,当使用 `explain format = 'verbose'` 语句查看一条 SQL 语句的查询计划时,如果该 SQL 语句使用了 binding,`explain` 语句会输出 warning 警告。此时可以通过查看 warning 了解该 SQL 语句使用了哪一条 binding。

```sql
-- 创建一个 global binding

create global binding for
select * from t
using
select * from t;

-- 使用 explain format = 'verbose' 语句查看 SQL 的执行计划,通过查看 warning 信息确认查询所使用的 binding

explain format = 'verbose' select * from t;
show warnings;
```

## 自动捕获绑定 (Baseline Capturing)

通过将 `tidb_capture_plan_baselines` 的值设置为 `on`(其默认值为 `off`)可以打开自动捕获绑定功能。
Expand Down Expand Up @@ -229,9 +259,11 @@ set global tidb_evolve_plan_baselines = on;

`tidb_evolve_plan_baselines` 的默认值为 `off`。

> **注意:**
> **警告:**
>
> 自动演进功能目前为实验特性,存在未知风险,不建议在生产环境中使用。
>
> 自动演进绑定功能目前不是 GA (Generally Available) 状态,不推荐在生产环境打开该功能
> 此变量开关已强制关闭,直到自动演进成为正式功能 GA (Generally Available)。如果你尝试打开开关,会产生报错。如果你已经在生产环境中使用了此功能,请尽快将它禁用。如发现 binding 状态不如预期,请与 PingCAP 的技术支持联系获取相关支持

在打开自动演进功能后,如果优化器选出的最优执行计划不在之前绑定的执行计划之中,会将其记录为待验证的执行计划。每隔 `bind-info-lease`(默认值为 `3s`),会选出一个待验证的执行计划,将其和已经绑定的执行计划中代价最小的比较实际运行时间。如果待验证的运行时间更优的话(目前判断标准是运行时间小于等于已绑定执行计划运行时间的 2/3),会将其标记为可使用的绑定。以下示例描述上述过程。

Expand Down