You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: partitioned-table.md
+13-16Lines changed: 13 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1700,13 +1700,13 @@ CREATE TABLE t (a varchar(20), b blob,
1700
1700
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
1701
1701
```
1702
1702
1703
-
####Global indexes
1703
+
### Global indexes
1704
1704
1705
1705
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.
1706
1706
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.
1708
1708
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.
1710
1710
1711
1711
> **Note:**
1712
1712
>
@@ -1719,13 +1719,14 @@ CREATE TABLE t1 (
1719
1719
col3 INTNOT NULL,
1720
1720
col4 INTNOT NULL,
1721
1721
UNIQUE KEY uidx12(col1, col2) GLOBAL,
1722
-
UNIQUE KEY uidx3(col3)
1722
+
UNIQUE KEY uidx3(col3),
1723
+
KEY idx1(col1) GLOBAL
1723
1724
)
1724
1725
PARTITION BY HASH(col3)
1725
1726
PARTITIONS 4;
1726
1727
```
1727
1728
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.
1729
1730
1730
1731
Note that a **clustered index** cannot be a global index, as shown in the following example:
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:
- If the `GLOBAL` keyword is not explicitly specified in the index definition, TiDB creates a local index by default.
1792
1795
- 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`:
- 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.
1800
1797
- Tables with global indexes do not support the `EXCHANGE PARTITION` operation.
1801
1798
- 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;
1931
1928
5 rows in set (0.00 sec)
1932
1929
```
1933
1930
1934
-
###Dynamic pruning mode
1931
+
## Dynamic pruning mode
1935
1932
1936
1933
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).
1937
1934
@@ -2136,7 +2133,7 @@ From example 2, you can see that in `dynamic` mode, the execution plan with Inde
2136
2133
2137
2134
Currently, `static` pruning mode does not support plan cache for both prepared and non-prepared statements.
2138
2135
2139
-
####Update statistics of partitioned tables in dynamic pruning mode
2136
+
### Update statistics of partitioned tables in dynamic pruning mode
0 commit comments