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

partition: add doc for global index #18217

Merged
merged 15 commits into from
Aug 19, 2024
62 changes: 60 additions & 2 deletions partitioned-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -1631,8 +1631,6 @@ PARTITIONS 4;

DDL 变更时,添加唯一索引也需要考虑到这个限制。比如创建了这样一个表:

{{< copyable "sql" >}}

```sql
CREATE TABLE t_no_pk (c1 INT, c2 INT)
PARTITION BY RANGE(c1) (
Expand Down Expand Up @@ -1666,6 +1664,66 @@ CREATE TABLE t (a varchar(20), b blob,
ERROR 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function
```

#### 全局索引

hfxsd marked this conversation as resolved.
Show resolved Hide resolved
如果你需要创建的唯一索引**不包含分区表达式中使用的所有列**,可以通过启用 [`tidb_enable_global_index`](/system-variables.md#tidb_enable_global_index-从-v760-版本开始引入) 系统变量来实现。

以前,分区表上的索引是为每个分区创建的,因此有一个限制,即所有唯一键都需要包含所有分区列。唯一性只能在每个分区内强制执行。全局索引将在表级别创建,因此无论分区如何,它都可以强制执行唯一性。注意,全局索引对分区管理有影响,`DROP`、`TRUNCATE` 和 `REORGANIZE PARTITION` 也需要管理表级全局索引。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

启用该变量后,任何不符合前述约束条件的唯一索引都将自动成为全局索引。

```sql
SET tidb_enable_global_index = ON;

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)
)
PARTITION BY HASH(col3)
PARTITIONS 4;
```

在上面示例中,唯一索引 `uidx12` 将隐式地成为全局索引,但 `uidx3` 仍是常规的唯一索引。

请注意,**聚簇索引**不能成为全局索引,如下例所示:

```sql
SET tidb_enable_global_index = ON;

CREATE TABLE t1 (
col1 INT NOT NULL,
col2 DATE NOT NULL,
PRIMARY KEY (col2) clustered
) PARTITION BY HASH(col1) PARTITIONS 5;
```

```
ERROR 1503 (HY000): A CLUSTERED INDEX must include all columns in the table's partitioning function
```

聚簇索引不能成为全局索引,是因为如果聚簇索引是全局索引,则表将不再分区。这是因为聚类索引的键也是记录键,应该位于分区级别上,但全局索引位于表级别,这就造成了冲突。
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

你可以通过查询 [`information_schema.tidb_indexes`](/information-schema/information-schema-tidb-indexes.md) 表并检查表结构来识别全局索引。

```sql
SELECT * FROM information_schema.tidb_indexes WHERE table_name='t1';
```

```
+--------------+------------+------------+----------+--------------+-------------+----------+---------------+------------+----------+------------+-----------+-----------+
| TABLE_SCHEMA | TABLE_NAME | NON_UNIQUE | KEY_NAME | SEQ_IN_INDEX | COLUMN_NAME | SUB_PART | INDEX_COMMENT | Expression | INDEX_ID | IS_VISIBLE | CLUSTERED | IS_GLOBAL |
+--------------+------------+------------+----------+--------------+-------------+----------+---------------+------------+----------+------------+-----------+-----------+
| test | t1 | 0 | uidx12 | 1 | col1 | NULL | | NULL | 1 | YES | NO | 1 |
| test | t1 | 0 | uidx12 | 2 | col2 | NULL | | NULL | 1 | YES | NO | 1 |
| test | t1 | 0 | uidx3 | 1 | col3 | NULL | | NULL | 2 | YES | NO | 0 |
+--------------+------------+------------+----------+--------------+-------------+----------+---------------+------------+----------+------------+-----------+-----------+
3 rows in set (0.00 sec)
```

### 关于函数的分区限制

只有以下函数可以用于分区表达式:
Expand Down
9 changes: 6 additions & 3 deletions placement-rules-in-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ CREATE PLACEMENT POLICY storageforhisotrydata CONSTRAINTS="[+node=history]";
CREATE PLACEMENT POLICY storagefornewdata CONSTRAINTS="[+node=new]";
CREATE PLACEMENT POLICY companystandardpolicy CONSTRAINTS="";

CREATE TABLE t1 (id INT, name VARCHAR(50), purchased DATE)
SET tidb_enable_global_index = ON;

CREATE TABLE t1 (id INT, name VARCHAR(50), purchased DATE, UNIQUE INDEX idx(id))
PLACEMENT POLICY=companystandardpolicy
PARTITION BY RANGE( YEAR(purchased) ) (
PARTITION p0 VALUES LESS THAN (2000) PLACEMENT POLICY=storageforhisotrydata,
hfxsd marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -295,12 +297,13 @@ PARTITION BY RANGE( YEAR(purchased) ) (
);
```

如果没有为表中的某个分区指定任何放置策略,该分区将尝试继承表上可能存在的策略。在上面示例中:
如果没有为表中的某个分区指定任何放置策略,该分区将尝试继承表上可能存在的策略。如果表有[全局索引](/partitioned-table.md#全局索引),索引将应用与表相同的放置策略。在上面示例中:

- `p0` 分区将会应用 `storageforhisotrydata` 策略
- `p4` 分区将会应用 `storagefornewdata` 策略
- `p1`、`p2`、`p3` 分区将会应用表 `t1` 的放置策略 `companystandardpolicy`
- 如果 `t1` 没有绑定任何策略,`p1`、`p2`、`p3` 会继承数据库或全局的默认策略
- 全局索引 `idx` 将应用与表 `t1` 相同的 `companystandardpolicy` 放置策略
- 如果没有为表 `t1`、`p1`、`p2` 和 `p3` 分区指定放置策略,全局索引 `idx` 将继承数据库默认策略或全局默认策略
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

给分区绑定放置策略后,你可以更改指定分区的放置策略。示例如下:

Expand Down
6 changes: 5 additions & 1 deletion system-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -1919,13 +1919,17 @@ mysql> SELECT job_info FROM mysql.analyze_jobs ORDER BY end_time DESC LIMIT 1;

### `tidb_enable_global_index` <span class="version-mark">从 v7.6.0 版本开始引入</span>

> **警告:**
>
> 该变量控制的功能为实验特性,不建议在生产环境中使用。该功能可能会在未事先通知的情况下发生变化或删除。如果发现 bug,请在 GitHub 上提 [issue](https://github.com/pingcap/tidb/issues) 反馈。

- 作用域:SESSION | GLOBAL
- 是否持久化到集群:是
- 是否受 Hint [SET_VAR](/optimizer-hints.md#set_varvar_namevar_value) 控制:否
- 类型:布尔型
- 默认值:`OFF`
- 可选值:`OFF`,`ON`
- 这个变量用于控制是否支持对分区表创建 `Global index`。`Global index` 当前正处于开发阶段,**不推荐修改该变量值**
- 该变量控制是否支持为分区表创建 [`全局索引`](/partitioned-table.md#全局索引)。启用此变量后,TiDB 将创建唯一索引,该索引不包含分区表达式中的所有列
hfxsd marked this conversation as resolved.
Show resolved Hide resolved

### `tidb_enable_non_prepared_plan_cache`

Expand Down