Skip to content

Commit 16e79fe

Browse files
authored
v8.5.4: partitioned-table: global index supports non-unique index (#19944) (#21879)
1 parent 0693593 commit 16e79fe

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

partitioned-table.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,13 +1699,13 @@ CREATE TABLE t (a varchar(20), b blob,
16991699
ERROR 8264 (HY000): Global Index is needed for index 'a', since the unique index is not including all partitioning columns, and GLOBAL is not given as IndexOption
17001700
```
17011701

1702-
#### Global indexes
1702+
### Global indexes
17031703

17041704
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.
17051705

1706-
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.
1706+
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 instead of looking up the local index for each partition, significantly improving query performance for non-partitioned keys. Starting from v8.5.4, non-unique indexes can also be created as global indexes.
17071707

1708-
To create a global index for a primary key or unique key, you can add the `GLOBAL` keyword in the index definition.
1708+
To create a global index, you can add the `GLOBAL` keyword in the index definition.
17091709

17101710
> **Note:**
17111711
>
@@ -1718,13 +1718,14 @@ CREATE TABLE t1 (
17181718
col3 INT NOT NULL,
17191719
col4 INT NOT NULL,
17201720
UNIQUE KEY uidx12(col1, col2) GLOBAL,
1721-
UNIQUE KEY uidx3(col3)
1721+
UNIQUE KEY uidx3(col3),
1722+
KEY idx1(col1) GLOBAL
17221723
)
17231724
PARTITION BY HASH(col3)
17241725
PARTITIONS 4;
17251726
```
17261727

1727-
In the preceding example, the unique index `uidx12` is a global index, while `uidx3` is a regular unique index.
1728+
In the preceding example, the unique index `uidx12` and non-unique index `idx1` are global indexes, while `uidx3` is a regular unique index.
17281729

17291730
Note that a **clustered index** cannot be a global index, as shown in the following example:
17301731

@@ -1756,7 +1757,8 @@ Create Table: CREATE TABLE `t1` (
17561757
`col3` int NOT NULL,
17571758
`col4` int NOT NULL,
17581759
UNIQUE KEY `uidx12` (`col1`,`col2`) /*T![global_index] GLOBAL */,
1759-
UNIQUE KEY `uidx3` (`col3`)
1760+
UNIQUE KEY `uidx3` (`col3`),
1761+
KEY `idx1` (`col1`) /*T![global_index] GLOBAL */
17601762
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
17611763
PARTITION BY HASH (`col3`) PARTITIONS 4
17621764
1 row in set (0.00 sec)
@@ -1775,26 +1777,23 @@ SELECT * FROM INFORMATION_SCHEMA.TIDB_INDEXES WHERE table_name='t1';
17751777
| test | t1 | 0 | uidx12 | 1 | col1 | NULL | | NULL | 1 | YES | NO | 1 |
17761778
| test | t1 | 0 | uidx12 | 2 | col2 | NULL | | NULL | 1 | YES | NO | 1 |
17771779
| test | t1 | 0 | uidx3 | 1 | col3 | NULL | | NULL | 2 | YES | NO | 0 |
1780+
| test | t1 | 1 | idx1 | 1 | col1 | NULL | | NULL | 3 | YES | NO | 1 |
17781781
+--------------+------------+------------+----------+--------------+-------------+----------+---------------+------------+----------+------------+-----------+-----------+
17791782
3 rows in set (0.00 sec)
17801783
```
17811784

1782-
When partitioning a non-partitioned table or repartitioning an already partitioned table, you can update the indexes to be global indexes or revert them to local indexes as needed:
1785+
When partitioning a non-partitioned table or repartitioning an already partitioned table, you can update the indexes to be global indexes or local indexes as needed.
1786+
1787+
For example, the following SQL statement repartitions table `t1` based on the `col1` column, updates the global indexes `uidx12` and `idx1` to local indexes, and updates the local index `uidx3` to a global index. Because `uidx3` is a unique index on the `col3` column, it must be a global index to ensure the uniqueness of `col3` across all partitions. `uidx12` and `idx1` are indexes on the `col1` column, which means they can be either global or local indexes.
17831788

17841789
```sql
1785-
ALTER TABLE t1 PARTITION BY HASH (col1) PARTITIONS 3 UPDATE INDEXES (uidx12 LOCAL, uidx3 GLOBAL);
1790+
ALTER TABLE t1 PARTITION BY HASH (col1) PARTITIONS 3 UPDATE INDEXES (uidx12 LOCAL, uidx3 GLOBAL, idx1 LOCAL);
17861791
```
17871792

1788-
##### Limitations of global indexes
1793+
#### Limitations of global indexes
17891794

17901795
- If the `GLOBAL` keyword is not explicitly specified in the index definition, TiDB creates a local index by default.
17911796
- The `GLOBAL` and `LOCAL` keywords only apply to partitioned tables and do not affect non-partitioned tables. In other words, there is no difference between a global index and a local index in non-partitioned tables.
1792-
- Currently, TiDB only supports creating unique global indexes on unique columns. If you need to create a global index on a non-unique column, you can include a primary key in the global index to create a composite index. For example, if the non-unique column is `col3` and the primary key is `col1`, you can use the following statement to create a global index on the non-unique column `col3`:
1793-
1794-
```sql
1795-
ALTER TABLE ... ADD UNIQUE INDEX(col3, col1) GLOBAL;
1796-
```
1797-
17981797
- DDL operations such as `DROP PARTITION`, `TRUNCATE PARTITION`, and `REORGANIZE PARTITION` also trigger updates to global indexes. These DDL operations need to wait for the global index updates to complete before returning results, which increases the execution time accordingly. This is particularly evident in data archiving scenarios, such as `DROP PARTITION` and `TRUNCATE PARTITION`. Without global indexes, these operations can typically complete immediately. However, with global indexes, the execution time increases as the number of indexes that need to be updated grows.
17991798
- Tables with global indexes do not support the `EXCHANGE PARTITION` operation.
18001799
- By default, the primary key of a partitioned table is a clustered index and must include the partition key. If you require the primary key to exclude the partition key, you can explicitly specify the primary key as a non-clustered global index when creating the table, for example, `PRIMARY KEY(col1, col2) NONCLUSTERED GLOBAL`.
@@ -1928,7 +1927,7 @@ select * from t;
19281927
5 rows in set (0.00 sec)
19291928
```
19301929

1931-
### Dynamic pruning mode
1930+
## Dynamic pruning mode
19321931

19331932
TiDB accesses partitioned tables in either `dynamic` or `static` mode. `dynamic` mode is used by default since v6.3.0. However, dynamic partitioning is effective only after the full table-level statistics, or global statistics, are collected. If you enable the `dynamic` pruning mode before global statistics collection is completed, TiDB remains in the `static` mode until global statistics are fully collected. For detailed information about global statistics, see [Collect statistics of partitioned tables in dynamic pruning mode](/statistics.md#collect-statistics-of-partitioned-tables-in-dynamic-pruning-mode).
19341933

@@ -2133,7 +2132,7 @@ From example 2, you can see that in `dynamic` mode, the execution plan with Inde
21332132

21342133
Currently, `static` pruning mode does not support plan cache for both prepared and non-prepared statements.
21352134

2136-
#### Update statistics of partitioned tables in dynamic pruning mode
2135+
### Update statistics of partitioned tables in dynamic pruning mode
21372136

21382137
1. Locate all partitioned tables:
21392138

0 commit comments

Comments
 (0)