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
+16-17Lines changed: 16 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1699,13 +1699,13 @@ CREATE TABLE t (a varchar(20), b blob,
1699
1699
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
1700
1700
```
1701
1701
1702
-
####Global indexes
1702
+
### Global indexes
1703
1703
1704
1704
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.
1705
1705
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.
1707
1707
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.
1709
1709
1710
1710
> **Note:**
1711
1711
>
@@ -1718,13 +1718,14 @@ CREATE TABLE t1 (
1718
1718
col3 INTNOT NULL,
1719
1719
col4 INTNOT NULL,
1720
1720
UNIQUE KEY uidx12(col1, col2) GLOBAL,
1721
-
UNIQUE KEY uidx3(col3)
1721
+
UNIQUE KEY uidx3(col3),
1722
+
KEY idx1(col1) GLOBAL
1722
1723
)
1723
1724
PARTITION BY HASH(col3)
1724
1725
PARTITIONS 4;
1725
1726
```
1726
1727
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.
1728
1729
1729
1730
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:
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.
- If the `GLOBAL` keyword is not explicitly specified in the index definition, TiDB creates a local index by default.
1791
1796
- 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`:
- 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.
1799
1798
- Tables with global indexes do not support the `EXCHANGE PARTITION` operation.
1800
1799
- 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;
1928
1927
5 rows in set (0.00 sec)
1929
1928
```
1930
1929
1931
-
###Dynamic pruning mode
1930
+
## Dynamic pruning mode
1932
1931
1933
1932
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).
1934
1933
@@ -2133,7 +2132,7 @@ From example 2, you can see that in `dynamic` mode, the execution plan with Inde
2133
2132
2134
2133
Currently, `static` pruning mode does not support plan cache for both prepared and non-prepared statements.
2135
2134
2136
-
####Update statistics of partitioned tables in dynamic pruning mode
2135
+
### Update statistics of partitioned tables in dynamic pruning mode
0 commit comments