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

sql: add fast mode #10560

Merged
merged 22 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply suggestions from code review
  • Loading branch information
hfxsd authored Jul 21, 2022
commit 8437115e435beb8cfd23c42c45b039a028ad659d
28 changes: 14 additions & 14 deletions develop/dev-guide-read-in-fast-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@ summary: 介绍通过使用 Fast Mode 来加速 AP 场景的查询的方法。
---
# Fast Mode

本章将介绍通过使用 Fast Mode 来加速 AP 场景的查询的方法。
本文档介绍通过使用 Fast Mode 来加速 Analytical Processing (AP) 场景的查询的方法。

## 概述

TiFlash 中的表默认属于 Normal Mode在 Normal Mode 下,对应表的查询结果提供了数据一致性的保证,来提供了精准的查询结果
TiFlash 中的表默认属于 Normal Mode在 Normal Mode 下,对应表的查询结果提供了数据一致性的保证,可以提供精准的查询结果

而针对 AP 中对查询结果精度可以容忍一定误差,而对查询性能有更高的要求的场景,我们可以将对应的表切换成 TiFlash 的 Fast Mode 模式后,再进行查询。
AP 中对查询结果精度可以容忍一定误差。对查询性能有更高要求的场景,可以将对应的表切换成 TiFlash 的 Fast Mode 模式后,再进行查询。

我们可以根据对不同表的不同查询结果需求,来回切换对应表的模式
你可以根据表的查询需求,使用对应的模式

## 切换成 Fast Mode 模式

所有表我们默认都是 Normal Mode。我们可以通过以下的语句将对应的表切换成 Fast Mode。
默认所有表都是 Normal Mode。你可以通过以下语句将对应的表切换成 Fast Mode。

{{< copyable "sql" >}}

```sql
ALTER TABLE table_name SET TIFALSH MODE FAST
hongyunyan marked this conversation as resolved.
Show resolved Hide resolved
```

我们可以通过以下语句来查看目前表的模式
可以通过以下语句来查看目前表的模式

```sql
SELECT table_mode FROM information_schema.tiflash_replica WHERE table_name = 'table_name' AND table_schema = 'database_name'
```

切换完成后,后续在 TiFlash 中的查询,都会在 Fast Mode 下进行,直到用下方的语句重新切换回 Normal Mode。
切换完成后,后续在 TiFlash 中的查询,都会在 Fast Mode 下进行。可以用下面语句重新切换回 Normal Mode。

```sql
ALTER TABLE table_name SET TIFALSH MODE NORMAL
hongyunyan marked this conversation as resolved.
Show resolved Hide resolved
```

模式的切换会在全局生效。我们对于 TiFlash 模式的切换,只有在有 TiFlash Replica 的表中才能生效。并且对于临时表、内存表、系统表、以及列名中含有非 utf-8 字符的表,目前都不支持 TiFlash 相关的操作,因此也不支持对其修改 TiFlash table mode。
模式的切换全局生效。对于 TiFlash 模式的切换,只会在有 TiFlash Replica 的表中才能生效。对于临时表、内存表、系统表、以及列名中含有非 utf-8 字符的表,目前都不支持 TiFlash 相关的操作,因此也不支持对其修改 TiFlash table mode。

你可以通过 [ALTER TABLE SET TIFLASH MODE](/sql-statements/sql-statement-set-tiflash-mode.md)了解更多的细节
了解更多信息,请参考 [ALTER TABLE SET TIFLASH MODE](/sql-statements/sql-statement-set-tiflash-mode.md)。

## 实现机制

TiFlash 中的存储层中,数据主要存放在 Delta 部分和 Stable 部分。
TiFlash 存储层的数据主要存放在 Delta 部分和 Stable 部分。

Normal Mode 中 TableScan 算子过程整体包括了以下步骤:

1. Read data : 在 Delta 层 和 Stable 层分别建立数据流,进行各自数据的读取。
2. Sort Merge : 将步骤1中建立的数据流进行合并,并且将数据按照 (handle,version) 顺序排列返回。
3. Range Filter : 对步骤2中的数据进行数据范围的过滤筛选并返回
4. MVCC + Column Filter:对步骤三中的数据进行 MVCC 的过滤,并过滤掉不需要的列进行数据的返回
2. Sort Merge : 将步骤 1 中建立的数据流进行合并,并且将数据按照 (handle, version) 顺序排列返回。
3. Range Filter : 对步骤 2 中的数据进行过滤筛选并返回
hfxsd marked this conversation as resolved.
Show resolved Hide resolved
4. MVCC + Column Filter:对步骤 3 中的数据进行 MVCC 过滤,同时过滤掉不需要的列并返回

Fast Mode 模式通过损失一定的数据一致性来获取更快的查询性能。我们简化 Fast Mode 中的 TableScan 流程,省略了上述过程中的第二步和第四步中 MVCC 的部分,从而提高查询的性能。
Fast Mode 模式通过损失一定的数据一致性来获取更快的查询性能。简化 Fast Mode 中的 TableScan 流程,省略了上述过程中的第 2 步和第 4 步中 MVCC 的部分,从而提高查询的性能。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved
hfxsd marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 10 additions & 7 deletions sql-statements/sql-statement-set-tiflash-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ summary: TiDB 数据库中 ALTER TABLE ... SET TIFLASH MODE ... 语句的使用
>
> 该语句目前是实验性功能,不建议在生产环境中使用。

使用 `ALTER TABLE ... SET TIFLASH MODE ...` 语句可以切换对应表在 TiFlash 中的模式状态。目前我们支持两种模式:`Normal Mode` 以及 `Fast Mode`。`Normal Mode`下,查询结果保证精度以及数据的一致性;`Fast Mode` 下则不保证数据精度和数据一致性,但提供更高效的查询性能。
使用 `ALTER TABLE ... SET TIFLASH MODE ...` 语句可以切换对应表在 TiFlash 中的模式状态。目前支持以下模式:

- `Normal Mode` 模式。该模式下保证查询结果精度以及数据的一致性。
- `Fast Mode` 模式。该模式不保证查询结果精度和数据一致性,但提供更高效的查询性能。

该语句执行时不会阻塞现有 SQL 语句的执行或 TiDB 功能的使用,包括事务、DDL、GC 等,也不会改变通过 SQL 语句访问获得的数据内容。该语句会在模式切换完毕后返回。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

该语句仅支持对表在 TiFlash 中的模式进行修改,因此模式修改后也仅对涉及到 TiFlash 表部分的读取产生对应模式的影响。

对表 TiFlash 模式的修改在表具有 TiFlash Replica 时才真实生效。若修改模式时,表的 TiFlash Replica 为空,则需等后续重新设置表的 TiFlash Replica 后修改的模式才真正生效。

我们可以通过 `information_schema.tiflash_replica` 这张系统表来查询对应表目前的 TiFlash table mode。
可以通过系统表 `information_schema.tiflash_replica` 查询对应表目前的 TiFlash table mode。

## 语法图

Expand All @@ -28,7 +31,7 @@ AlterTableSetTiFlashModeStmt ::=

## 示例

假设我们目前有张 `test` 表,它具有一个 TiFlash 副本。
假设表 `test` 有一个 TiFlash 副本。

{{< copyable "sql" >}}

Expand All @@ -40,7 +43,7 @@ CREATE TABLE test (a INT NOT NULL, b INT);
ALTER TABLE test SET TIFLASH REPLICA 1;
```

`test` 表默认的 mode 为 Normal Mode,我们可以通过以下语句进行查询
`test` 表默认的 mode 为 Normal Mode,可以通过以下语句进行查询。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

```sql
SELECT table_mode FROM information_schema.tiflash_replica WHERE table_name = 'test' AND table_schema = 'test'
Expand All @@ -54,7 +57,7 @@ SELECT table_mode FROM information_schema.tiflash_replica WHERE table_name = 'te
+------------+
```

当我们想启用 fast mode 来对表 test 进行查询时,执行以下语句来切换模式, 并可以重新查询当前表的 mode。
如果要启用 Fast Mode 查询表 test,执行以下语句来切换模式, 并可以查询当前表的 mode。

```sql
ALTER TABLE test SET tiflash mode FAST
Expand All @@ -70,7 +73,7 @@ SELECT table_mode FROM information_schema.tiflash_replica WHERE table_name = 'te
+------------+
```

当我们想重新使用 normal mode 模式时,执行以下语句将表切换回 normal 模式
如果想重新使用 Normal Mode 模式,执行以下语句将表切换

```sql
ALTER TABLE test SET tiflash mode NORMAL
Expand All @@ -82,7 +85,7 @@ ALTER TABLE test SET tiflash mode NORMAL

## TiDB Binlog 及 TiCDC 兼容性

当 下游也为 TiDB 时,`ALTER TABLE ...SET TiFLASH MODE ..` 会被 TiDB Binlog 同步到下游。其他状态下,Binlog 和 TiCDC 都不会同步该语句。
当下游也为 TiDB 时,`ALTER TABLE ...SET TiFLASH MODE ..` 会被 TiDB Binlog 同步到下游。其他状态下,Binlog 和 TiCDC 都不会同步该语句。

## 另请参阅

Expand Down