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 #19382

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Changes from 4 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
25 changes: 20 additions & 5 deletions partitioned-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ ALTER TABLE members REMOVE PARTITIONING
To partition an existing non-partitioned table or modify the partition type of an existing partitioned table, you can use the following statement, which copies all rows and recreates the indexes online according to the new partition definitions:

```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}...])]
```

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

When partitioning a non-partitioned table or repartitioning an already partitioned table, you can update the indexes to be global or local as needed:

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

[Partition pruning](/partition-pruning.md) is an optimization which is based on a very simple idea - do not scan the partitions that do not match.
Expand Down Expand Up @@ -1472,10 +1487,10 @@ This section introduces some restrictions and limitations on partitioned tables

### Partitioning keys, primary keys and unique keys

This section discusses the relationship of partitioning keys with primary keys and unique keys. The rule governing this relationship can be expressed as follows: **Every unique key on the table must use every column in the table's partitioning expression**. This also includes the table's primary key, because it is by definition a unique key.
This section discusses the relationship of partitioning keys with primary keys and unique keys. The rule governing this relationship is as follows: every unique key on the table, including the primary key, must use every column in the table's partitioning expression, because the primary key is also a unique key by definition.
lilin90 marked this conversation as resolved.
Show resolved Hide resolved

> **Note:**
>
>
> You can ignore this rule when using [global indexes](#global-indexes).

For example, the following table creation statements are invalid:
Expand Down Expand Up @@ -1689,9 +1704,9 @@ ERROR 8264 (HY000): Global Index is needed for index 'a', since the unique index

Before the introduction of global indexes, TiDB created a local index for each partition, leading to [a limitation](#partitioning-keys-primary-keys-and-unique-keys) that primary keys and unique keys had to include the partition key to ensure data uniqueness. Additionally, when querying data across multiple partitions, TiDB needed to scan the data of each partition to return results.

To address these issues, TiDB introduces the global indexes feature in v8.3.0. A global index covers the data of the entire table with a single index, allowing primary keys and unique keys to maintain global uniqueness without including all partition keys. Moreover, global indexes can access data across multiple partitions in a single operation, significantly improving query performance for non-partitioned keys.
To address these issues, TiDB introduces the global indexes feature in v8.3.0. A global index covers the data of the entire table with a single index, allowing primary keys and unique keys to maintain global uniqueness without including all partition keys. Moreover, global indexes can access index data across multiple partitions in a single operation, significantly improving query performance for non-partitioned keys instead of looking up in one local index for each partition.

To create a global index for a primary key or unique key that **does not include all the columns used in the partition expressions**, you can enable the [`tidb_enable_global_index`](/system-variables.md#tidb_enable_global_index-new-in-v760) system variable and add the `GLOBAL` keyword in the index definition.
To create a global index for a primary key or unique key, you can add the `GLOBAL` keyword in the index definition.

> **Note:**
>
Expand Down
Loading