Skip to content

Commit b2f80fc

Browse files
committed
partitioned-table: global index supports non-unique index
1 parent 511b92a commit b2f80fc

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

partitioned-table.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,13 +1700,13 @@ CREATE TABLE t (a varchar(20), b blob,
17001700
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
17011701
```
17021702

1703-
#### Global indexes
1703+
### Global indexes
17041704

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

1707-
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.
1707+
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. Starting from v9.0.0, non-unique indexes can also be created as global indexes.
17081708

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

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

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

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

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

17831786
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:
17841787

17851788
```sql
1786-
ALTER TABLE t1 PARTITION BY HASH (col1) PARTITIONS 3 UPDATE INDEXES (uidx12 LOCAL, uidx3 GLOBAL);
1789+
ALTER TABLE t1 PARTITION BY HASH (col1) PARTITIONS 3 UPDATE INDEXES (uidx12 LOCAL, uidx3 GLOBAL, idx1 LOCAL);
17871790
```
17881791

1789-
##### Limitations of global indexes
1792+
#### Limitations of global indexes
17901793

17911794
- If the `GLOBAL` keyword is not explicitly specified in the index definition, TiDB creates a local index by default.
17921795
- 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.
1793-
- 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`:
1794-
1795-
```sql
1796-
ALTER TABLE ... ADD UNIQUE INDEX(col3, col1) GLOBAL;
1797-
```
1798-
17991796
- 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.
18001797
- Tables with global indexes do not support the `EXCHANGE PARTITION` operation.
18011798
- 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`.
@@ -1931,7 +1928,7 @@ select * from t;
19311928
5 rows in set (0.00 sec)
19321929
```
19331930

1934-
### Dynamic pruning mode
1931+
## Dynamic pruning mode
19351932

19361933
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).
19371934

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

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

2139-
#### Update statistics of partitioned tables in dynamic pruning mode
2136+
### Update statistics of partitioned tables in dynamic pruning mode
21402137

21412138
1. Locate all partitioned tables:
21422139

0 commit comments

Comments
 (0)