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 9 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
32 changes: 30 additions & 2 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' 状态并更新完缓存)的回收清除操作。
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved

### 查看绑定

{{< 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,30 @@ SHOW [GLOBAL | SESSION] BINDINGS [ShowLikeOrWhere];
| collation | 排序规则 |
| source | 创建方式,包括 manual (由 `create [global] binding` 生成)、capture(由 tidb 自动创建生成)和 evolve (由 tidb 自动演进生成) |

### 绑定的排查
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved

{{< copyable "sql" >}}

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

该语句使用系统变量 [last_plan_from_binding](https://docs.pingcap.com/zh/tidb/stable/system-variables#last_plan_from_binding-%E4%BB%8E-v40-%E7%89%88%E6%9C%AC%E5%BC%80%E5%A7%8B%E5%BC%95%E5%85%A5) 显示上一条执行的语句所使用的执行计划是否来自 binding 的执行计划。
TomShawn marked this conversation as resolved.
Show resolved Hide resolved

另外,当我们使用 explain 语句查看 SQL 的查询计划时,如果该 SQL 使用了 Binding,explain 语句会产生 warning,我们可以通过查看 warning 了解 SQL 使用了哪一条 Binding。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain verbose?


```
TomShawn marked this conversation as resolved.
Show resolved Hide resolved
-- 创建一个 global binding
create global binding for
select * from t
using
select * from t;

-- 使用 explain 语句查看 SQL 的执行计划,通过查看 warning 信息确认查询所使用的 Binding
explain select * from t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain verbose?

show warnings;
```

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

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

> **注意:**
Reminiscent marked this conversation as resolved.
Show resolved Hide resolved
>
> 自动演进绑定功能目前不是 GA (Generally Available) 状态,不推荐在生产环境打开该功能
> 自动演进功能目前为实验特性,存在未知风险并需要继续优化,不建议在生产环境中使用。此开关将被禁用直到该功能到达 GA (Generally Available) 状态,如果尝试打开开关,会产生报错。如果你已经在生产环境中使用了此功能,请尽快将它关闭,如发现 Binding 状态不如预期,请与 PingCAP 的技术支持联系获取相关支持
TomShawn marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down