Skip to content

feat: add docs about tidb cloud #20529

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

Open
wants to merge 1 commit into
base: release-8.1
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
857 changes: 857 additions & 0 deletions TOC-tidb-cloud.md

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions accelerated-table-creation.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
---
title: 提升 TiDB 建表性能
aliases: ['/zh/tidb/stable/ddl-v2/']
summary: 介绍 TiDB 加速建表中的概念、原理、实现和影响
title: TiDB 加速表创建
aliases: ['/tidb/stable/ddl-v2/']
summary: 了解 TiDB 中创建表的性能优化的概念、原理和实现细节
---

# 提升 TiDB 建表性能
# TiDB 加速表创建

TiDB v7.6.0 引入了系统变量 [`tidb_ddl_version`](https://docs.pingcap.com/zh/tidb/v7.6/system-variables#tidb_ddl_version-从-v760-版本开始引入) 实现支持加速建表,可提升大批量建表的速度。从 v8.0.0 开始,该系统变量更名为 [`tidb_enable_fast_create_table`](/system-variables.md#tidb_enable_fast_create_table-从-v800-版本开始引入)。
TiDB v7.6.0 引入了系统变量 [`tidb_ddl_version`](https://docs.pingcap.com/tidb/v7.6/system-variables#tidb_enable_fast_create_table-new-in-v800) 以支持加速表创建,提高批量创建表的效率。从 v8.0.0 开始,该系统变量被重命名为 [`tidb_enable_fast_create_table`](/system-variables.md#tidb_enable_fast_create_table-new-in-v800)。

TiDB 中,对元数据对象的更改采用的是 online DDL 算法(即在线异步变更算法)。所有的 DDL Job 会提交到 `mysql.tidb_ddl_job` 表里,由 owner 节点拉取 DDL Job,执行完 online DDL 算法中的各个阶段后,将该 DDL Job 标记为已完成,移入 `mysql.tidb_ddl_history` 表中。因此 DDL 只能在 owner 节点执行,无法线性拓展
TiDB 使用在线异步 schema 变更算法来更改元数据。所有 DDL 任务都提交到 `mysql.tidb_ddl_job` 表中,由 owner 节点拉取 DDL 任务执行。在执行完在线 DDL 算法的每个阶段后,DDL 任务被标记为完成并移动到 `mysql.tidb_ddl_history` 表中。因此DDL 语句只能在 owner 节点上执行,无法线性扩展

然而,对于某些 DDL 而言,并不需要严格按照 online DDL 算法执行。如 `CREATE TABLE` 语句,Job 只有 `none` 和 `public` 两个状态,因此可以简化 DDL 的运行流程,使得建表语句可以在非 owner 节点执行,从而实现加速建表
然而,对于某些 DDL 语句,不需要严格遵循在线 DDL 算法。例如,`CREATE TABLE` 语句的任务只有 `none` 和 `public` 两个状态。因此,TiDB 可以简化 DDL 的执行过程,在非 owner 节点上执行 `CREATE TABLE` 语句以加速表创建

> **警告:**
>
> TiDB 加速建表目前为实验特性,不建议在生产环境中使用。该功能可能会在未事先通知的情况下发生变化或删除。如果发现 bug,请在 GitHub 上提 [issue](https://github.com/pingcap/tidb/issues) 反馈
> 该功能目前是实验性功能,不建议在生产环境中使用。该功能可能会在没有事先通知的情况下发生变更或被移除。如果发现 bug,请通过在 GitHub 上提交 [issue](https://github.com/pingcap/tidb/issues) 来反馈

## 与 TiDB 工具的兼容性

- [TiCDC](/ticdc/ticdc-overview.md) 暂不支持同步通过 TiDB 加速创建的表
- [TiCDC](https://docs.pingcap.com/tidb/stable/ticdc-overview) 不支持复制通过 `tidb_enable_fast_create_table` 创建的表

## 限制

TiDB 加速建表目前仅适用于 [`CREATE TABLE`](/sql-statements/sql-statement-create-table.md) 语句,且该建表语句不带任何外键约束
目前,表创建性能优化仅支持 [`CREATE TABLE`](/sql-statements/sql-statement-create-table.md) 语句,且该语句不能包含任何外键约束

## 使用方法
## 使用 `tidb_enable_fast_create_table` 加速表创建

你可以通过设置系统变量 [`tidb_enable_fast_create_table`](/system-variables.md#tidb_enable_fast_create_table-从-v800-版本开始引入) 的值来开启或关闭加速建表的功能
你可以通过指定系统变量 [`tidb_enable_fast_create_table`](/system-variables.md#tidb_enable_fast_create_table-new-in-v800) 的值来启用或禁用表创建性能优化

要开启该功能,将该变量的值设置为 `ON`:
要启用表创建性能优化,将该变量的值设置为 `ON`:

```sql
SET GLOBAL tidb_enable_fast_create_table = ON;
```

要关闭该功能,将该变量的值设置为 `OFF`:
要禁用表创建性能优化,将该变量的值设置为 `OFF`:

```sql
SET GLOBAL tidb_enable_fast_create_table = OFF;
```

## 实现原理

TiDB 加速建表的实现步骤如下
表创建性能优化的详细实现原理如下

1. 创建 `CREATE TABLE` Job
1. 创建 `CREATE TABLE` 任务

通过解析 `CREATE TABLE` 语句生成相应的 DDL Job
通过解析 `CREATE TABLE` 语句生成相应的 DDL 任务

2. 执行 `CREATE TABLE` Job
2. 执行 `CREATE TABLE` 任务

由接收该 `CREATE TABLE` 语句的 TiDB 节点直接执行建表语句,将表结构持久化到 TiKV。同时,将 `CREATE TABLE` Job 标记为已完成,插入到 `mysql.tidb_ddl_history` 表中。
接收 `CREATE TABLE` 语句的 TiDB 节点直接执行该语句,然后将表结构持久化到 TiKV。同时,将 `CREATE TABLE` 任务标记为完成并插入到 `mysql.tidb_ddl_history` 表中。

3. 同步表信息。

TiDB 通知其他节点同步该新建的表结构
TiDB 通知其他节点同步新创建的表结构
32 changes: 22 additions & 10 deletions agg-distinct-optimization.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
title: Distinct 优化
summary: 本文介绍了对于 DISTINCT 的优化,包括简单 DISTINCT 和聚合函数 DISTINCT 的优化。简单的 DISTINCT 通常会被优化成 GROUP BY 来执行。而带有 DISTINCT 的聚合函数会在 TiDB 侧单线程执行,可以通过系统变量或 TiDB 配置项控制优化器是否执行。在优化后,DISTINCT 被下推到了 Coprocessor,在 HashAgg 里新增了一个 group by 列
title: DISTINCT 优化
summary: 介绍 TiDB 查询优化器中的 `distinct` 优化
---

# Distinct 优化
# DISTINCT 优化

本文档介绍可用于 `DISTINCT` 的优化,包括简单 `DISTINCT` 和聚合函数 `DISTINCT` 的优化
本文档介绍 TiDB 查询优化器中的 `distinct` 优化,包括 `SELECT DISTINCT` 和聚合函数中的 `DISTINCT`。

## 简单 DISTINCT
## `SELECT` 语句中的 `DISTINCT` 修饰符

通常简单的 `DISTINCT` 会被优化成 GROUP BY 来执行。例如:
`DISTINCT` 修饰符用于从结果集中删除重复的行。`SELECT DISTINCT` 会被转换为 `GROUP BY`,例如:

```sql
mysql> explain select DISTINCT a from t;
mysql> explain SELECT DISTINCT a from t;
+--------------------------+---------+-----------+---------------+-------------------------------------------------------+
| id | estRows | task | access object | operator info |
+--------------------------+---------+-----------+---------------+-------------------------------------------------------+
Expand All @@ -23,11 +23,23 @@ mysql> explain select DISTINCT a from t;
3 rows in set (0.00 sec)
```

## 聚合函数 DISTINCT
## 聚合函数中的 `DISTINCT` 选项

通常来说,带有 `DISTINCT` 的聚合函数会单线程的在 TiDB 侧执行。使用系统变量 [`tidb_opt_distinct_agg_push_down`](/system-variables.md#tidb_opt_distinct_agg_push_down) 或者 TiDB 的配置项 [distinct-agg-push-down](/tidb-configuration-file.md#distinct-agg-push-down) 控制优化器是否执行带有 `DISTINCT` 的聚合函数(比如 `select count(distinct a) from t`)下推到 Coprocessor 的优化操作
通常,带有 `DISTINCT` 选项的聚合函数在 TiDB 层使用单线程执行模型执行

在以下示例中,`tidb_opt_distinct_agg_push_down` 开启前,TiDB 需要从 TiKV 读取所有数据,并在 TiDB 侧执行 `disctinct`。`tidb_opt_distinct_agg_push_down` 开启后,`distinct a` 被下推到了 Coprocessor,在 `HashAgg_5` 里新增了一个 `group by` 列 `test.t.a`。
<CustomContent platform="tidb">

[`tidb_opt_distinct_agg_push_down`](/system-variables.md#tidb_opt_distinct_agg_push_down) 系统变量或 TiDB 中的 [`distinct-agg-push-down`](/tidb-configuration-file.md#distinct-agg-push-down) 配置项控制是否重写 distinct 聚合查询并将其下推到 TiKV 或 TiFlash Coprocessor。

</CustomContent>

<CustomContent platform="tidb-cloud">

TiDB 中的 [`tidb_opt_distinct_agg_push_down`](/system-variables.md#tidb_opt_distinct_agg_push_down) 系统变量控制是否重写 distinct 聚合查询并将其下推到 TiKV 或 TiFlash Coprocessor。

</CustomContent>

以下面的查询为例说明这个优化。`tidb_opt_distinct_agg_push_down` 默认是禁用的,这意味着聚合函数在 TiDB 层执行。通过将其值设置为 `1` 启用此优化后,`count(distinct a)` 中的 `distinct a` 部分会被下推到 TiKV 或 TiFlash Coprocessor:在 TiKV Coprocessor 中有一个 HashAgg_5 用于删除列 a 上的重复值。这可能会减少 TiDB 层 `HashAgg_8` 的计算开销。

```sql
mysql> desc select count(distinct a) from test.t;
Expand Down
Loading
Loading