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 15 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: 2 additions & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- [Follower Read](/develop/dev-guide-use-follower-read.md)
- [Stale Read](/develop/dev-guide-use-stale-read.md)
- [HTAP 查询](/develop/dev-guide-hybrid-oltp-and-olap-queries.md)
- [Fast Mode](/develop/dev-guide-read-in-fast-mode.md)
- 事务
- [概览](/develop/dev-guide-transaction-overview.md)
- [乐观事务和悲观事务](/develop/dev-guide-optimistic-and-pessimistic-transaction.md)
Expand Down Expand Up @@ -599,6 +600,7 @@
- [`ALTER PLACEMENT POLICY`](/sql-statements/sql-statement-alter-placement-policy.md)
- [`ALTER TABLE`](/sql-statements/sql-statement-alter-table.md)
- [`ALTER TABLE COMPACT`](/sql-statements/sql-statement-alter-table-compact.md)
- [`ALTER TABLE SET TIFLASH MODE`](/sql-statements/sql-statement-set-tiflash-mode.md)
- [`ALTER USER`](/sql-statements/sql-statement-alter-user.md)
- [`ANALYZE TABLE`](/sql-statements/sql-statement-analyze-table.md)
- [`BACKUP`](/sql-statements/sql-statement-backup.md)
Expand Down
56 changes: 56 additions & 0 deletions develop/dev-guide-read-in-fast-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: Fast Mode
summary: 介绍通过使用 Fast Mode 来加速 AP 场景的查询的方法。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved
---

# Fast Mode

本文档介绍通过使用 Fast Mode 来加速 Analytical Processing (AP) 场景中查询的方法。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

TiFlash 支持以下模式:

- `Normal Mode` 模式。默认模式。该模式能够保证查询结果精度以及数据一致性。
- `Fast Mode` 模式。该模式不保证查询结果精度和数据一致性,但可以实现更高效的查询性能。

AP 对查询结果精度可以容忍一定误差。如果对查询性能有更高要求,可以将对应的表切换成 TiFlash 的 Fast Mode 模式进行查询。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

模式的切换全局生效。对于 TiFlash 模式的切换,只会在有 TiFlash Replica 的表中才能生效。对于临时表、内存表、系统表、以及列名中含有非 utf-8 字符的表,都不支持 TiFlash 相关的操作,因此也不支持对其修改 TiFlash table mode。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

了解更多信息,请参考 [ALTER TABLE SET TIFLASH MODE](/sql-statements/sql-statement-set-tiflash-mode.md)。

## 切换成 Fast Mode 模式

默认情况下,所有表都是 Normal Mode。通过以下语句来查看目前表的模式。

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

通过以下语句将对应的表切换成 Fast Mode。

{{< copyable "sql" >}}

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

切换完成后,后续在 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 存储层的数据主要存放在 Delta 层和 Stable 层。

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

1. Read data : 在 Delta 层 和 Stable 层分别建立数据流,进行各自数据的读取。
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 流程省略了上述 Normal Mode 过程中的第 2 步和第 4 步中 MVCC 的部分,从而提高查询性能。
3 changes: 2 additions & 1 deletion information-schema/information-schema-tiflash-replica.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ DESC tiflash_replica;
| LOCATION_LABELS | varchar(64) | YES | | NULL | |
| AVAILABLE | tinyint(1) | YES | | NULL | |
| PROGRESS | double | YES | | NULL | |
| TABLE_MODE | varchar(64) | YES | | NULL | |
+-----------------+-------------+------+------+---------+-------+
7 rows in set (0.01 sec)
8 rows in set (0.01 sec)
```
92 changes: 92 additions & 0 deletions sql-statements/sql-statement-set-tiflash-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: ALTER TABLE ... SET TIFLASH MODE ...
summary: TiDB 数据库中 ALTER TABLE ... SET TIFLASH MODE ... 语句的使用概况。
---

# `ALTER TABLE ... SET TIFLASH MODE ...`

> **警告:**
>
> 该语句目前是实验性功能,不建议在生产环境中使用。

使用 `ALTER TABLE ... SET TIFLASH MODE ...` 语句可以切换对应表在 TiFlash 中的模式状态。目前支持以下模式:

- `Normal Mode` 模式。默认模式。该模式能够保证查询结果精度以及数据一致性。
- `Fast Mode` 模式。该模式不保证查询结果精度和数据一致性,但可以实现更高效的查询性能。

该语句执行时不会阻塞现有 SQL 语句的执行或 TiDB 功能的使用,包括事务、DDL、GC 等,也不会改变通过 SQL 语句访问获得的数据内容。该语句会在模式切换完毕后正常结束。

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

对表 TiFlash 模式的修改在表具有 TiFlash Replica 时才真实生效。若修改模式时,表的 TiFlash Replica 为空,则需等后续重新设置表的 TiFlash Replica 后修改的模式才真正生效。你可以使用 [`ALTER TABLE ... SET TIFLASH REPLICA ...`](/sql-statements/sql-statement-alter-table.md) 语句来设置表的 TiFlash Replica。

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

## 语法图

```ebnf+diagram
AlterTableSetTiFlashModeStmt ::=
'ALTER' 'TABLE' TableName 'SET' 'TIFLASH' 'MODE' mode
```

## 示例

假设表 `test` 有一个 TiFlash 副本。

{{< copyable "sql" >}}

```sql
USE TEST;

CREATE TABLE test (a INT NOT NULL, b INT);

ALTER TABLE test SET TIFLASH REPLICA 1;
```

`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'
```

```
+------------+
| table_mode |
+------------+
| NORMAL |
+------------+
```

如果要启用 Fast Mode 查询 `test` 表,执行以下语句来切换模式, 并可以查询当前表的 mode。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

```sql
ALTER TABLE test SET tiflash mode FAST

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

```
+------------+
| table_mode |
+------------+
| FAST |
+------------+
```

如果想重新使用 Normal Mode 模式,执行以下语句切换。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

```sql
ALTER TABLE test SET tiflash mode NORMAL
```

## MySQL 兼容性

`ALTER TABLE ...SET TiFLASH MODE ..` 语法是 TiDB 引入的对标准 SQL 语法的扩展。尽管没有对应的 MySQL 语法,你仍然可通过 MySQL 各版本客户端,或各个遵循 MySQL 协议的数据库驱动执行该语句。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

## TiDB Binlog 及 TiCDC 兼容性

当下游也为 TiDB 时,`ALTER TABLE ...SET TiFLASH MODE ..` 会被 TiDB Binlog 同步到下游。其他场景下,TiDB Binlog 和 TiCDC 都不会同步该语句。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

## 另请参阅

- [ALTER TABLE](/sql-statements/sql-statement-alter-table.md)