tencent cloud

TDSQL Boundless

Global Index

Download
Focus Mode
Font Size
Last updated: 2026-07-10 14:51:07
A Global Secondary Index (GSI) is a cross-partition index type provided by TDSQL Boundless partition tables. Unlike the default LOCAL index, where index data is stored independently per partition, a GSI stores index data globally and in order across all partitions. This enables the optimizer to locate the partition containing the data directly via the GSI, without scanning all partitions.

Supported Version

This applies to TDSQL Boundless version V21.6.3.0 and later.

Limitations

Global secondary indexes can only be created on partition tables. They are not supported on non-partition tables.
A global secondary index cannot be used as a primary key index.
Global indexes cannot be created when columnar storage nodes are present.
REMOVE PARTITIONING converts a global index into a regular index for a single table.
For performing DROP PARTITION or TRUNCATE PARTITION operations on a partition table that contains a global index, see Partition DDL and the UPDATE GLOBAL INDEXES Clause.

Scenarios

Non-partition key queries: For example, consider an order table hash-partitioned by user_id, where queries are also frequently performed by order_no. If a LOCAL index is used, a query by order_no fans out to all partitions. Switching to a GSI allows the query to directly locate the partition based on the actual data.
CREATE TABLE orders (
user_id BIGINT NOT NULL,
order_no VARCHAR(32) NOT NULL,
amount DECIMAL(10,2),
PRIMARY KEY (user_id, order_no),
INDEX g_idx_order_no (order_no) GLOBAL -- A global secondary index
) PARTITION BY HASH(user_id) PARTITIONS 4;

-- During a query, the optimizer uses the GSI to directly locate the partition, eliminating the need for a full partition scan.
SELECT user_id, amount FROM orders WHERE order_no = 'NO20240601001';
Global uniqueness constraint: For example, a user table is partitioned by user_id, but the email and phone fields must be unique across the entire table. A LOCAL unique index can only guarantee uniqueness within a partition and cannot enforce it across partitions. In this case, a global unique index is used to achieve this requirement.
CREATE TABLE users (
user_id BIGINT NOT NULL,
email VARCHAR(128) NOT NULL,
phone VARCHAR(20) NOT NULL,
PRIMARY KEY (user_id),
UNIQUE INDEX g_uk_email (email) GLOBAL, -- Globally unique: the email field is unique across the entire table
UNIQUE INDEX g_uk_phone (phone) GLOBAL -- Globally unique: the phone field is unique across the entire table
) PARTITION BY HASH(user_id) PARTITIONS 4;

INSERT INTO users (user_id, email, phone) VALUES (1, 'test@example.com', '13800000001');

INSERT INTO users (user_id, email, phone) VALUES (2, 'test@example.com', '13800000002');
-- ERROR 1062 (23000): Duplicate entry 'test@example.com' for key 'users.g_uk_email'.

INSERT INTO users (user_id, email, phone) VALUES (3, 'test2@example.com', '13800000001');
-- ERROR 1062 (23000): Duplicate entry '13800000001' for key 'users.g_uk_phone'.

Partition DDL and the UPDATE GLOBAL INDEXES Clause

Because a GSI's index data is stored globally across all partitions, DDL operations that alter partition data, such as DROP PARTITION or TRUNCATE PARTITION, can cause the original GSI to become inconsistent with the underlying data. TDSQL Boundless uses the UPDATE GLOBAL INDEXES clause to control how the GSI is handled in this situation.
Syntax
GSI Handling
Result
Without UPDATE GLOBAL INDEXES
The GSI is marked as unusable.
The DDL operation executes quickly, but subsequent queries that use the GSI will report errors, requiring manual rebuilding.
With UPDATE GLOBAL INDEXES
Synchronously rebuilds the GSI during the DDL process.
The DDL operation takes longer, but the GSI remains available throughout.
The following example uses an order table that is RANGE-partitioned by order_date and contains the GSI g_idx_order_no to fully demonstrate the differences between the two syntaxes.
-- Create a table: Perform RANGE partitioning by year on the order_date column, and create a global secondary index on the order_no column
CREATE TABLE orders (
order_no VARCHAR(32) NOT NULL,
order_date DATE NOT NULL,
user_id BIGINT NOT NULL,
amount DECIMAL(10,2),
PRIMARY KEY (order_date, order_no),
INDEX g_idx_order_no (order_no) GLOBAL
) PARTITION BY RANGE (YEAR(order_date)) (
PARTITION p2022 VALUES LESS THAN (2023),
PARTITION p2023 VALUES LESS THAN (2024),
PARTITION p2024 VALUES LESS THAN (2025),
PARTITION p2025 VALUES LESS THAN (2026)
);

-- Insert test data (with order_date being 2023, the data lands in the p2023 partition)
INSERT INTO orders (order_no, order_date, user_id, amount) VALUES ('NO20230601001', '2023-06-01', 1001, 99.50);

-- Syntax 1: Without the UPDATE GLOBAL INDEXES clause. The partition DDL executes successfully and returns an alarm, but the global secondary indexes will be marked as unavailable.
ALTER TABLE orders TRUNCATE PARTITION p2023;
-- Query OK, 0 rows affected, 1 warning

-- View alarm details by using SHOW WARNINGS:
SHOW WARNINGS;
-- +---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-- | Level | Code | Message |
-- +---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-- | Warning | 8510 | SQLEngine system error 'Global secondary index 'g_idx_order_no' has been marked as unusable after TRUNCATE PARTITION without UPDATE GLOBAL INDEXES. Use ALTER TABLE ... UPDATE GLOBAL INDEXES to rebuild.' |
-- +---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- After an index is marked as unavailable, forcing a query to use that global secondary index will return an index does not exist error.
SELECT * FROM orders FORCE INDEX(g_idx_order_no) WHERE order_no = 'NO20230601001';
-- ERROR 1176 (42000): Key 'g_idx_order_no' doesn't exist in table 'orders'.

-- Recovery method 1: Execute a partition DDL that includes the UPDATE GLOBAL INDEXES clause to rebuild the global secondary indexes while modifying the partitions.
ALTER TABLE orders TRUNCATE PARTITION p2024 UPDATE GLOBAL INDEXES;

-- Recovery method 2: Delete and then recreate the global secondary index.
ALTER TABLE orders DROP INDEX g_idx_order_no;
ALTER TABLE orders ADD INDEX g_idx_order_no (order_no) GLOBAL;

-- Syntax 2: With the UPDATE GLOBAL INDEXES clause. The global secondary indexes are rebuilt synchronously while the partition DDL is executed, keeping the indexes available throughout the process.
ALTER TABLE orders TRUNCATE PARTITION p2025 UPDATE GLOBAL INDEXES;
Usage Suggestions:
Avoid performing DROP PARTITION / TRUNCATE PARTITION on tables with GSIs whenever possible. These partition DDL operations can cause global indexes to become inconsistent with the underlying data, either rendering the GSIs unavailable or significantly increasing DDL execution time due to synchronous GSI rebuilds. If your business has frequent requirements for adding or deleting partitions, you should evaluate whether GSIs are truly necessary during the table design phase.
If you must execute such partition DDL and still rely on GSI queries afterwards, directly include the UPDATE GLOBAL INDEXES clause to complete the operation in one step and avoid an intermediate availability window.
If you prioritize DDL speed and can accept performing a separate index rebuild later, you can omit this clause and subsequently execute ALTER TABLE DROP / ADD INDEX to rebuild the index.
This clause applies only to the DROP PARTITION and TRUNCATE PARTITION partition operations.
This clause only applies to global secondary indexes and does not affect local indexes. If no global secondary indexes exist on the table, this clause will not have any additional effect.

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback