首批通过分布式安全可靠测评,为关键业务系统打造
DROP INDEX
更新时间:2026-04-06 12:08:11
描述
该语句用来删除表上的索引。当索引过多时,维护开销增大,因此需要删除不必要的索引。
注意
- 删除索引后,该索引将无法再用于优化查询性能,请谨慎操作。
- 删除索引不会影响表中的数据。
- 支持删除普通索引、唯一索引、全文索引等各类索引。
语法
DROP INDEX index_name ON table_name;
参数解释
| 参数 | 描述 |
|---|---|
| index_name | 指定要删除的索引名称。 |
| table_name | 指定索引所属的表名。 |
示例
- 创建表并创建索引,然后删除索引。
obclient> CREATE TABLE test(c1 INT PRIMARY KEY, c2 INT, c3 INT);
obclient> CREATE INDEX test_index ON test(c2);
obclient> SHOW INDEX FROM test;
查询结果如下:
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| test | 0 | PRIMARY | 1 | c1 | A | NULL | NULL | NULL | | BTREE | available | | YES |
| test | 1 | test_index | 1 | c2 | A | NULL | NULL | NULL | YES | BTREE | available | | YES |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
2 rows in set
删除索引:
obclient> DROP INDEX test_index ON test;
obclient> SHOW INDEX FROM test;
查询结果如下:
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| test | 0 | PRIMARY | 1 | c1 | A | NULL | NULL | NULL | | BTREE | available | | YES |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
1 row in set
- 删除唯一索引。删除唯一索引后,原本由该索引约束的唯一性将失效,允许插入重复值。
obclient> CREATE TABLE test(c1 INT PRIMARY KEY, c2 INT, UNIQUE INDEX idx(c2));
obclient> INSERT INTO test(c1, c2) VALUES(1, 1);
obclient> DROP INDEX idx ON test;
obclient> INSERT INTO test(c1, c2) VALUES(2, 1);
- 删除全文索引。
obclient> CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200),
content TEXT,
FULLTEXT INDEX ctx(title, content)
);
obclient> DROP INDEX ctx ON articles;