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

Added REORGANIZE PARTITION as a new feature #11385

Merged
merged 41 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9192be3
Added REORGANIZE PARTITION as experimental feature
mjonss Nov 16, 2022
7e49adb
Update partitioned-table.md
mjonss Nov 21, 2022
7d10fc6
Update partitioned-table.md
mjonss Nov 21, 2022
77be0d4
Update partitioned-table.md
mjonss Nov 21, 2022
67ca95d
Update partitioned-table.md
mjonss Nov 21, 2022
7d0aa33
Update partitioned-table.md
mjonss Nov 21, 2022
2e31255
Update partitioned-table.md
mjonss Nov 21, 2022
fd62e10
Update partitioned-table.md
mjonss Nov 21, 2022
b7ccc5f
Update partitioned-table.md
mjonss Nov 21, 2022
ffe8791
Update partitioned-table.md
mjonss Nov 21, 2022
045f72e
Update partitioned-table.md
mjonss Nov 21, 2022
5511e72
Removed experimental, currently aiming for GA directly.
mjonss Nov 21, 2022
62e4ec5
Apply suggestions from code review
qiancai Nov 24, 2022
96e1a36
Update partitioned-table.md
mjonss Nov 24, 2022
9ef2e82
Minor update, adding one example and changed error message
mjonss Nov 24, 2022
bcf0df2
Clarified matching existing rows
mjonss Nov 24, 2022
af17e92
Removed duplicated text (it has been moved below as an own example)
mjonss Nov 24, 2022
b00bbfe
minor change
mjonss Nov 24, 2022
ec1665b
merge -> change
mjonss Nov 24, 2022
ddca1ad
minor change and reshuffling of text
mjonss Nov 24, 2022
f8b639c
Fixed broken link
mjonss Nov 24, 2022
223be31
Update partitioned-table.md
mjonss Nov 25, 2022
54d5022
Update partitioned-table.md
mjonss Nov 25, 2022
a3a8f5c
Update partitioned-table.md
mjonss Nov 25, 2022
4caa693
Update partitioned-table.md
mjonss Nov 25, 2022
14f7dab
Update partitioned-table.md
mjonss Nov 25, 2022
1f5bf94
Update partitioned-table.md
mjonss Nov 25, 2022
0354cdc
fix CI errors
qiancai Nov 25, 2022
0df8974
Update partitioned-table.md
qiancai Nov 28, 2022
1f09de9
update mysql compatibility
qiancai Nov 28, 2022
d8f7b69
wording and format updates
qiancai Nov 28, 2022
0df353c
rephrase the sentences for range changes
qiancai Nov 28, 2022
cdb39f2
Update partitioned-table.md
mjonss Jan 31, 2023
3ddb0a0
Update partitioned-table.md
mjonss Mar 21, 2023
2f4b113
Update partitioned-table.md
mjonss Mar 21, 2023
6df5ebb
wording updates
qiancai Mar 22, 2023
7daa018
wording updates
qiancai Mar 22, 2023
67a599a
wording updates by placing the RANGE type first
qiancai Mar 22, 2023
9a700aa
Merge remote-tracking branch 'upstream/master' into pr/11385
qiancai Mar 23, 2023
008a553
Apply suggestions from code review
qiancai Mar 27, 2023
3a224fa
Apply suggestions from code review
qiancai Mar 27, 2023
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
11 changes: 5 additions & 6 deletions mysql-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,13 @@ In TiDB, all supported DDL changes are performed online. Compared with DDL opera
* The `ALGORITHM={INSTANT,INPLACE,COPY}` syntax functions only as an assertion in TiDB, and does not modify the `ALTER` algorithm. See [`ALTER TABLE`](/sql-statements/sql-statement-alter-table.md) for further details.
* Adding/Dropping the primary key of the `CLUSTERED` type is unsupported. For more details about the primary key of the `CLUSTERED` type, refer to [clustered index](/clustered-indexes.md).
* Different types of indexes (`HASH|BTREE|RTREE|FULLTEXT`) are not supported, and will be parsed and ignored when specified.
* Table Partitioning supports `HASH`, `RANGE`, `LIST`, and `KEY` partitioning types. Currently, the `KEY` partition type does not support partition statements with an empty partition column list. For an unsupported partition type, TiDB returns `Warning: Unsupported partition type %s, treat as normal table`, where `%s` is the specific partition type.
* Table Partitioning also supports `ADD`, `DROP`, and `TRUNCATE` operations. Other partition operations are ignored. The following Table Partition syntaxes are not supported:
- `PARTITION BY KEY`
- `PARTITION BY LINEAR KEY`
* TiDB supports `HASH`, `RANGE`, `LIST`, and `KEY` partitioning types. Currently, the `KEY` partition type does not support partition statements with an empty partition column list. For an unsupported partition type, TiDB returns `Warning: Unsupported partition type %s, treat as normal table`, where `%s` is the specific unsupported partition type.
* Partitioned tables support `ADD`, `DROP`, `TRUNCATE`, and `REORGANIZE` operations. Other partition operations are ignored. The following syntaxes are not supported for partitioned tables:

- `SUBPARTITION`
- `{CHECK|TRUNCATE|OPTIMIZE|REPAIR|IMPORT|DISCARD|REBUILD|REORGANIZE|COALESCE} PARTITION`
- `{CHECK|TRUNCATE|OPTIMIZE|REPAIR|IMPORT|DISCARD|REBUILD|COALESCE} PARTITION`
mjonss marked this conversation as resolved.
Show resolved Hide resolved

For more details, see [Partitioning](/partitioned-table.md).
For more details, see [Partitioning](/partitioned-table.md).

### Analyze table

Expand Down
161 changes: 117 additions & 44 deletions partitioned-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,12 @@ For Key partitioning, the way of handling `NULL` value is consistent with that o

## Partition management

For `LIST` and `RANGE` partitioned tables, you can add and drop partitions using the `ALTER TABLE <table name> ADD PARTITION (<partition specification>)` or `ALTER TABLE <table name> DROP PARTITION <list of partitions>` statement.
For `RANGE`, `RANGE COLUMNS`, `LIST`, and `LIST COLUMNS` partitioned tables, you can manage the partitions as follows:

For `LIST` and `RANGE` partitioned tables, `REORGANIZE PARTITION` is not yet supported.
- Add partitions using the `ALTER TABLE <table name> ADD PARTITION (<partition specification>)` statement.
- Drop partitions using the `ALTER TABLE <table name> DROP PARTITION <list of partitions>` statement.
- Remove all data from specified partitions using the `ALTER TABLE <table name> TRUNCATE PARTITION <list of partitions>` statement. The logic of `TRUNCATE PARTITION` is similar to [`TRUNCATE TABLE`](/sql-statements/sql-statement-truncate.md) but it is for partitions.
- Merge, split, or make other changes to the partitions using the `ALTER TABLE <table name> REORGANIZE PARTITION <list of partitions> INTO (<new partition definitions>)` statement.
qiancai marked this conversation as resolved.
Show resolved Hide resolved

For `HASH` and `KEY` partitioned tables, only `ALTER TABLE ... TRUNCATE PARTITION` is supported, while `COALESCE PARTITION` and `ADD PARTITION` are not yet supported.

Expand All @@ -842,79 +845,149 @@ In addition, there are limitations on the compatibility of `EXCHANGE PARTITION`
- TiCDC: TiCDC replicates the `EXCHANGE PARTITION` operation when both partitioned and non-partitioned tables have primary keys or unique keys. Otherwise, TiCDC will not replicate the operation.
- TiDB Lightning and BR: do not perform the `EXCHANGE PARTITION` operation during import using TiDB Lightning or during restore using BR.

### Range partition management
### Manage Range, Range COLUMNS, List, and List COLUMNS partitions

Create a partitioned table:

{{< copyable "sql" >}}
This section uses the partitioned tables created by the following SQL statements as examples to show you how to manage Range and List partitions.

```sql
CREATE TABLE members (
id INT,
fname VARCHAR(25),
lname VARCHAR(25),
dob DATE
id int,
fname varchar(255),
lname varchar(255),
dob date,
data json
)

PARTITION BY RANGE( YEAR(dob) ) (
PARTITION p0 VALUES LESS THAN (1980),
PARTITION p1 VALUES LESS THAN (1990),
PARTITION p2 VALUES LESS THAN (2000)
);
PARTITION BY RANGE (YEAR(dob)) (
PARTITION pBefore1950 VALUES LESS THAN (1950),
PARTITION p1950 VALUES LESS THAN (1960),
PARTITION p1960 VALUES LESS THAN (1970),
PARTITION p1970 VALUES LESS THAN (1980),
PARTITION p1980 VALUES LESS THAN (1990),
PARTITION p1990 VALUES LESS THAN (2000));

CREATE TABLE member_level (
id int,
level int,
achievements json
)
PARTITION BY LIST (level) (
PARTITION l1 VALUES IN (1),
PARTITION l2 VALUES IN (2),
PARTITION l3 VALUES IN (3),
PARTITION l4 VALUES IN (4),
PARTITION l5 VALUES IN (5));
```

Drop a partition:

{{< copyable "sql" >}}
#### Drop partitions

```sql
ALTER TABLE members DROP PARTITION p2;
```
ALTER TABLE members DROP PARTITION p1990;

ALTER TABLE member_level DROP PARTITION l5;
```
Query OK, 0 rows affected (0.03 sec)

#### Truncate partitions

```sql
ALTER TABLE members TRUNCATE PARTITION p1980;

ALTER TABLE member_level TRUNCATE PARTITION l4;
```

Empty a partition:
#### Add partitions

{{< copyable "sql" >}}
```sql
ALTER TABLE members ADD PARTITION (PARTITION `p1990to2010` VALUES LESS THAN (2010));

ALTER TABLE member_level ADD PARTITION (PARTITION l5_6 VALUES IN (5,6));
```

For a Range partitioned table, `ADD PARTITION` will append new partitions after the last existing partition. Compared with the existing partitions, the value defined in `VALUES LESS THAN` for new partitions must be greater. Otherwise, an error is reported:

```sql
ALTER TABLE members TRUNCATE PARTITION p1;
ALTER TABLE members ADD PARTITION (PARTITION p1990 VALUES LESS THAN (2000));
```

```
Query OK, 0 rows affected (0.03 sec)
ERROR 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition
```

> **Note:**
>
> `ALTER TABLE ... REORGANIZE PARTITION` is currently unsupported in TiDB.

Add a partition:
#### Reorganize partitions

{{< copyable "sql" >}}
Split a partition:
mjonss marked this conversation as resolved.
Show resolved Hide resolved

```sql
ALTER TABLE members ADD PARTITION (PARTITION p3 VALUES LESS THAN (2010));
```
ALTER TABLE members REORGANIZE PARTITION `p1990to2010` INTO
(PARTITION p1990 VALUES LESS THAN (2000),
PARTITION p2000 VALUES LESS THAN (2010),
PARTITION p2010 VALUES LESS THAN (2020),
PARTITION p2020 VALUES LESS THAN (2030),
PARTITION pMax VALUES LESS THAN (MAXVALUE));

When partitioning tables by Range, `ADD PARTITION` can be only appended to the very end of a partition list. If it is appended to an existing Range partition, an error is reported:
ALTER TABLE member_level REORGANIZE PARTITION l5_6 INTO
(PARTITION l5 VALUES IN (5),
PARTITION l6 VALUES IN (6));
```

{{< copyable "sql" >}}
Merge partitions:
mjonss marked this conversation as resolved.
Show resolved Hide resolved

```sql
ALTER TABLE members
ADD PARTITION (
PARTITION n VALUES LESS THAN (1970));
```
ALTER TABLE members REORGANIZE PARTITION pBefore1950,p1950 INTO (PARTITION pBefore1960 VALUES LESS THAN (1960));

ALTER TABLE member_level REORGANIZE PARTITION l1,l2 INTO (PARTITION l1_2 VALUES IN (1,2));
```
ERROR 1463 (HY000): VALUES LESS THAN value must be strictly »
increasing for each partition

Change the partitioning scheme definition:

```sql
ALTER TABLE members REORGANIZE PARTITION pBefore1960,p1960,p1970,p1980,p1990,p2000,p2010,p2020,pMax INTO
(PARTITION p1800 VALUES LESS THAN (1900),
PARTITION p1900 VALUES LESS THAN (2000),
PARTITION p2000 VALUES LESS THAN (2100));

ALTER TABLE member_level REORGANIZE PARTITION l1_2,l3,l4,l5,l6 INTO
(PARTITION lOdd VALUES IN (1,3,5),
PARTITION lEven VALUES IN (2,4,6));
```

### Hash partition management
When reorganizing partitions, you need to note the following key points:

- Reorganizing partitions (including merging or splitting partitions) can change the listed partitions into a new set of partition definitions but cannot change the type of partitioning (for example, change the List type to the Range type, or change the Range COLUMNS type to the Range type).

- For a Range partition table, you can reorganize only adjacent partitions in it.

```sql
ALTER TABLE members REORGANIZE PARTITION p1800,p2000 INTO (PARTITION p2000 VALUES LESS THAN (2100));
```

```
ERROR 8200 (HY000): Unsupported REORGANIZE PARTITION of RANGE; not adjacent partitions
```

- For a Range partitioned table, to modify the end of the range, the new end defined in `VALUES LESS THAN` must cover the existing rows in the last partition. Otherwise, existing rows no longer fit and an error is reported:

```sql
INSERT INTO members VALUES (313, "John", "Doe", "2022-11-22", NULL);
ALTER TABLE members REORGANIZE PARTITION p2000 INTO (PARTITION p2000 VALUES LESS THAN (2050)); -- This statement will work as expected, because 2050 covers the existing rows.
ALTER TABLE members REORGANIZE PARTITION p2000 INTO (PARTITION p2000 VALUES LESS THAN (2020)); -- This statement will fail with an error, because 2022 does not fit in the new range.
```

```
ERROR 1526 (HY000): Table has no partition for value 2022
```

- For a List partitioned table, to modify the set of values defined for a partition, the new definition must cover the existing values in that partition. Otherwise, an error is reported:

```sql
INSERT INTO member_level (id, level) values (313, 6);
ALTER TABLE member_level REORGANIZE PARTITION lEven INTO (PARTITION lEven VALUES IN (2,4));
```

```
ERROR 1526 (HY000): Table has no partition for value 6
```

### Manage Hash partitions

Unlike Range partitioning, `DROP PARTITION` is not supported in Hash partitioning.

Expand All @@ -923,7 +996,7 @@ Currently, `ALTER TABLE ... COALESCE PARTITION` is not supported in TiDB as well
{{< copyable "sql" >}}

```sql
alter table members optimize partition p0;
ALTER TABLE MEMBERS OPTIMIZE PARTITION p0;
```

```sql
Expand Down