# Method 1: Using the CREATE INDEX statementCREATE [UNIQUE] INDEXindex_name ON tbl_name (column_names)[GLOBAL][index_option][algorithm_option];# Method 2: Using the ALTER TABLE statementALTER 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}
COMMENT 'string': Add a comment for the index.VISIBLE | INVISIBLE: Sets whether the index is visible to the optimizer.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.# Create a test tableCREATE 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 algorithmCREATE 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 defaultCREATE UNIQUE INDEX idx_v4 ON sbtest1 (v4);ALTER TABLE sbtest1 ADD INDEX idx_v3 (v3);
フィードバック