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

docs: update documents related to Plan Cache #9553

Merged
merged 24 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion dashboard/dashboard-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ QPS 及 Latency 监控依赖于集群中已正常部署 Prometheus 监控实例

### 慢查询页面显示 `invalid connection` 错误

可能的原因是你开启了 TiDB 的 `prepared-plan-cache` 功能。`prepared-plan-cache` 是实验性功能,在某些版本的 TiDB 中可能无法正常运行,开启后可能会导致 TiDB Dashboard(及其他应用)出现该问题。可以通过修改 [TiDB 配置文件](/tidb-configuration-file.md#prepared-plan-cache)来关闭 `prepared-plan-cache` 功能
可能的原因是你开启了 TiDB 的 `prepared-plan-cache` 功能。`prepared-plan-cache` 是实验性功能,在某些版本的 TiDB 中可能无法正常运行,开启后可能会导致 TiDB Dashboard(及其他应用)出现该问题。你可以通过系统变量 [`tidb_enable_prepared_plan_cache`](/system-variables.md#tidb_enable_prepared_plan_cache-从-v610-版本开始引入) 关闭这项功能

### 界面提示 `集群中未启动必要组件 NgMonitoring`

Expand Down
1 change: 1 addition & 0 deletions grafana-tidb-dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ aliases: ['/docs-cn/dev/grafana-tidb-dashboard/','/docs-cn/dev/reference/key-mon
- Execution Duration:执行 SQL 语句执行计划耗时。
- Expensive Executor OPS:每秒消耗系统资源比较多的算子。包括 Merge Join、Hash Join、Index Look Up Join、Hash Agg、Stream Agg、Sort、TopN 等。
- Queries Using Plan Cache OPS:每秒使用 Plan Cache 的查询数量。
- Plan Cache Miss OPS:每秒出现 Plan Cache Miss 的数量。

- Distsql
- Distsql Duration:Distsql 处理的时长
Expand Down
2 changes: 1 addition & 1 deletion optimizer-hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ SELECT /*+ READ_CONSISTENT_REPLICA() */ * FROM t;

`IGNORE_PLAN_CACHE()` 提示优化器在处理当前 `prepare` 语句时不使用 plan cache。

该 Hint 用于在 [prepared-plan-cache](/tidb-configuration-file.md#prepared-plan-cache) 开启的场景下临时对某类查询禁用 plan cache。
该 Hint 用于在 [Prepared Plan Cache](/sql-prepared-plan-cache.md) 开启的场景下临时对某类查询禁用 plan cache。

以下示例强制该 `prepare` 语句不使用 plan cache:

Expand Down
19 changes: 18 additions & 1 deletion sql-prepared-plan-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ key 中任何一项变动(如切换数据库,重命名 `Prepare` 语句,
- 考虑到不同 `Execute` 的参数会不同,执行计划缓存为了保证适配性会禁止一些和具体参数值密切相关的激进查询优化手段,导致对特定的一些参数值,查询计划可能不是最优。比如查询的过滤条件为 `where a > ? and a < ?`,第一次 `Execute` 时参数分别为 2 和 1,考虑到这两个参数下次执行时可能会是 1 和 2,优化器不会生成对当前参数最优的 `TableDual` 执行计划。
- 如果不考虑缓存失效和淘汰,一份执行计划缓存会对应各种不同的参数取值,理论上也会导致某些取值下执行计划非最优。比如查询过滤条件为 `where a < ?`,假如第一次执行 `Execute` 时用的参数值为 1,此时优化器生成最优的 `IndexScan` 执行计划放入缓存,在后续执行 `Exeucte` 时参数变为 10000,此时 `TableScan` 可能才是更优执行计划,但由于执行计划缓存,执行时还是会使用先前生成的 `IndexScan`。因此执行计划缓存更适用于查询较为简单(查询编译耗时占比较高)且执行计划较为固定的业务场景。

目前执行计划缓存功能默认关闭,可以通过打开配置文件中 [`prepared-plan-cache` 项](/tidb-configuration-file.md#prepared-plan-cache)启用这项功能
目前执行计划缓存功能默认打开,可以通过变量 [`tidb_enable_prepared_plan_cache`](/system-variables.md#tidb_enable_prepared_plan_cache-从-v610-版本开始引入) 启用或关闭这项功能

> **注意:**
>
Expand Down Expand Up @@ -132,6 +132,23 @@ MySQL [test]> select @@last_plan_from_cache;
1 row in set (0.00 sec)
```

## Prepared Plan Cache 的内存管理

使用 Prepared Plan Cache 会有一定的内存开销,在内部测试中,平均每个缓存计划会消耗 100 KiB 内存,且目前 Plan Cache 是 `SESSION` 级别的,因此总内存消耗大致为 `SESSION 个数 * SESSION 平均缓存计划个数 * 100KiB`。

比如目前 TiDB 实例的 `SESSION` 并发数是 50,平均每个 `SESSION` 大致缓存 100 个计划,则总内存开销为 `50 * 100 * 100KiB` 约等于 `512MB`。

目前可以通过变量 `tidb_prepared_plan_cache_size` 来设置每个 `SESSION` 最多缓存的计划数量,针对不同的环境,推荐的设置如下:

- TiDB Server 实例内存阈值 <= 64 GiB 时,`tidb_prepared_plan_cache_size = 50`
- TiDB Server 实例内存阈值 > 64 GiB 时,`tidb_prepared_plan_cache_size = 100`

当 TiDB Server 的内存余量小于一定阈值时,会触发 Plan Cache 的内存保护机制,此时会对一些缓存的计划进行逐出。

目前该阈值由变量 `tidb_prepared_plan_cache_memory_guard_ratio` 控制,默认为 0.1,即 10%,也就是当剩余内存不足 10%(使用内存超过 90%)时,会触发此机制。

由于内存限制,Plan Cache 可能出现 Cache Miss 的情况,可以通过 Grafana 中的 [`Plan Cache Miss OPS` 监控](/grafana-tidb-dashboard.md)查看。

## 手动清空计划缓存

通过执行 `ADMIN FLUSH [SESSION | INSTANCE] PLAN_CACHE` 语句,你可以手动清空计划缓存。
Expand Down
26 changes: 26 additions & 0 deletions system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,14 @@ MPP 是 TiFlash 引擎提供的分布式计算框架,允许节点之间的数
- 默认值:0
- 这个变量用于控制是否开启 Apply 算子并发,并发数由 `tidb_executor_concurrency` 变量控制。Apply 算子用来处理关联子查询且默认无并发,所以执行速度较慢。打开 Apply 并发开关可增加并发度,提高执行速度。目前默认关闭。

### `tidb_enable_prepared_plan_cache` <span class="version-mark">从 v6.1.0 版本开始引入</span>

- 作用域:GLOBAL
- 是否持久化到集群:是
- 默认值:`ON`
- 这个变量用来控制是否开启 [Prepared Plan Cache](/sql-prepared-plan-cache.md)。开启后,对 `Prepare`、`Execute` 请求的执行计划会进行缓存,以便在后续执行时跳过查询计划优化这个步骤,获得性能上的提升。
- 在 v6.1.0 之前这个开关通过 TiDB 配置文件 (`prepared-plan-cache.enabled`) 进行配置,升级到 v6.1.0 时会自动继承原有设置。

### `tidb_enable_pseudo_for_outdated_stats` <span class="version-mark">从 v5.3.0 版本开始引入</span>

- 作用域:SESSION | GLOBAL
Expand Down Expand Up @@ -1510,6 +1518,24 @@ explain select * from t where age=5;
- 范围:`[0, 1]`
- 这个变量用来控制是否在 profile 输出中标记出对应的 SQL 语句,用于定位和排查性能问题。

### `tidb_prepared_plan_cache_memory_guard_ratio` <span class="version-mark">从 v6.1.0 版本开始引入</span>

- 作用域:GLOBAL
- 是否持久化到集群:是
- 默认值:`0.1`
- 范围:`[0, 1]`
- 这个变量用来控制 Prepared Plan Cache 触发内存保护机制的阈值,具体可见 [Prepared Plan Cache 的内存管理](/sql-prepared-plan-cache.md#prepared-plan-cache-的内存管理)。
- 在 v6.1.0 之前这个开关通过 TiDB 配置文件 (`prepared-plan-cache.memory-guard-ratio`) 进行配置,升级到 v6.1.0 时会自动继承原有设置。

### `tidb_prepared_plan_cache_size` <span class="version-mark">从 v6.1.0 版本开始引入</span>

- 作用域:GLOBAL
- 是否持久化到集群:是
- 默认值:`100`
- 范围:`[1, 100000]`
- 这个变量用来控制单个 `SESSION` 的 Prepared Plan Cache 最多能够缓存的计划数量,具体可见 [Prepared Plan Cache 的内存管理](/sql-prepared-plan-cache.md#prepared-plan-cache-的内存管理)。
- 在 v6.1.0 之前这个开关通过 TiDB 配置文件 (`prepared-plan-cache.capacity`) 进行配置,升级到 v6.1.0 时会自动继承原有设置。

### `tidb_projection_concurrency`

> **警告:**
Expand Down
22 changes: 0 additions & 22 deletions tidb-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,28 +470,6 @@ TiDB 配置文件比命令行参数支持更多的选项。你可以在 [config/
+ 用于控制 TiDB 是否开启统计信息缓存的内存上限。
+ 默认值:false

## prepared-plan-cache

prepare 语句的 [`plan cache`](/sql-prepared-plan-cache.md) 设置。

### `enabled`

+ 开启 prepare 语句的 plan cache。
+ 默认值:false

### `capacity`

+ 缓存语句的数量。
+ 默认值:1000
+ 类型为 uint,小于 0 的值会被转化为大整数。

### `memory-guard-ratio`

+ 用于防止 prepare plan cache 的内存用量超过 performance.server-memory-quota。当 prepare plan cache 的内存用量超过 server-memory-quota * (1 - prepared-plan-cache.memory-guard-ratio) 时,TiDB 会剔除 LRU 中的元素。
+ 默认值:0.1
+ 最小值:0
+ 最大值:1

## opentracing

opentracing 的相关的设置。
Expand Down