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

partitioned-table: remove some restrictions on global index #19053

Merged
merged 4 commits into from
Nov 22, 2024
Merged
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
21 changes: 18 additions & 3 deletions partitioned-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ ALTER TABLE members REMOVE PARTITIONING
要对现有的非分区表进行分区或修改现有分区表的分区类型,你可以使用以下语句。该语句在执行时,将根据新的分区定义复制表中的所有行,并在线重新创建索引:

```sql
ALTER TABLE <table_name> PARTITION BY <new partition type and definitions>
ALTER TABLE <table_name> PARTITION BY <new partition type and definitions> [UPDATE INDEXES (<index name> {GLOBAL|LOCAL}[ , <index name> {GLOBAL|LOCAL}...])]
```

示例:
Expand All @@ -1176,6 +1176,21 @@ ALTER TABLE member_level PARTITION BY RANGE(level)
PARTITION pMax VALUES LESS THAN (MAXVALUE));
```

对普通表进行分区或者对分区表进行重新分区时,可以根据需要将索引更新为全局索引或普通索引:

```sql
CREATE TABLE t1 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
col3 INT NOT NULL,
col4 INT NOT NULL,
UNIQUE KEY uidx12(col1, col2),
UNIQUE KEY uidx3(col3)
);

ALTER TABLE t1 PARTITION BY HASH (col1) PARTITIONS 3 UPDATE INDEXES (uidx12 LOCAL, uidx3 GLOBAL);
```

## 分区裁剪

有一个优化叫做[“分区裁剪”](/partition-pruning.md),它基于一个非常简单的概念:不需要扫描那些匹配不上的分区。
Expand Down Expand Up @@ -1674,9 +1689,9 @@ ERROR 8264 (HY000): Global Index is needed for index 'a', since the unique index

在引入全局索引 (Global Index) 之前,TiDB 会为每个分区创建一个局部索引 (Local Index),即一个分区对应一个局部索引。这种索引方式存在一个[使用限制](#分区键主键和唯一键):主键和唯一键必须包含所有的分区键,以确保数据的全局唯一性。此外,当查询的数据跨越多个分区时,TiDB 需要扫描各个分区的数据才能返回结果。

为解决这些问题,TiDB 从 v8.3.0 开始引入全局索引。全局索引能覆盖整个表的数据,使得主键和唯一键在不包含分区键的情况下仍能保持全局唯一性。同时,全局索引可以在一次操作中访问多个分区的数据,显著提升了针对非分区键的查询性能。
为解决这些问题,TiDB 从 v8.3.0 开始引入全局索引。全局索引能覆盖整个表的数据,使得主键和唯一键在不包含分区键的情况下仍能保持全局唯一性。此外,全局索引可以在一次操作中访问多个分区的索引数据,而无需对每个分区的本地索引逐一查找,显著提升了针对非分区键的查询性能。

如果你需要创建的唯一索引**不包含分区表达式中使用的所有列**,可以通过在索引定义中添加 `GLOBAL` 关键字来实现。
如果你需要为主键或唯一键创建全局索引,可以通过在索引定义中添加 `GLOBAL` 关键字来实现。

> **注意:**
>
Expand Down