tencent cloud

TDSQL Boundless

CREATE INDEX

Download
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-07-13 16:54:09

Overview

Indexes are the sorting of data, and TDSQL Boundless uses indexes to quickly locate data. This section provides the syntax and examples for creating indexes.
Creating secondary indexes in TDSQL Boundless is an online operation by default and does not block read and write operations on the table. For detailed information on Online DDL, see Online DDL Overview.

Create an Index on an Existing Table

# Method 1: Using the CREATE INDEX statement
CREATE [UNIQUE] INDEX
index_name ON tbl_name (column_names)
[GLOBAL]
[index_option]
[algorithm_option];

# Method 2: Using the ALTER TABLE statement
ALTER TABLE tbl_name ADD
{ [UNIQUE] {INDEX | KEY}
| PRIMARY KEY
}
index_name (column_names)
[GLOBAL]
[index_option]
[algorithm_option];
index_option: {
| COMMENT 'string'
| {VISIBLE | INVISIBLE}
}

algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}
Parameter description
Index option (index_option)
COMMENT 'string': Add a comment for the index.
VISIBLE | INVISIBLE: Sets whether the index is visible to the optimizer.
Algorithm option (algorithm_option)
ALGORITHM [=] {DEFAULT | INPLACE | COPY}: Specifies the algorithm for index creation.
DEFAULT: The system automatically selects the optimal algorithm.
INPLACE: Online creation without blocking read and write operations (recommended).
COPY: Creates an index by copying table data without blocking read and write operations by default.
GLOBAL option: For details, see Global Index.

Examples

# Create a test table
CREATE TABLE sbtest1 (id int, v1 int, v2 int, v3 int, v4 int);

# As of iteration 21.2.3, online add pk is not yet supported.
ALTER TABLE sbtest1 ADD PRIMARY KEY(id), ALGORITHM = COPY;
ERROR 8528 (HY000): Online alter table tdsql.sbtest1 failed with 'Not support table without primary key', please set variable 'tdsql_use_online_copy_ddl' to 'false' if no write during alter is acceptable.

# Display index settings for COMMENT, visibility, and algorithm
CREATE UNIQUE INDEX idx_v1 ON sbtest1 (v1) COMMENT 'v1_index' INVISIBLE ALGORITHM = INPLACE;
ALTER TABLE sbtest1 ADD INDEX idx_v2 (v2) COMMENT 'v2_index' VISIBLE, ALGORITHM = INPLACE;

# Uses the INPLACE algorithm by default
CREATE UNIQUE INDEX idx_v4 ON sbtest1 (v4);
ALTER TABLE sbtest1 ADD INDEX idx_v3 (v3);

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan