当索引过多时,维护开销会增大,您可以根据需要删除不必要的索引。
语法
通过 DROP INDEX 语句删除索引:
DROP INDEX index_name ON table_name;ALTER TABLE 时删除索引:
ALTER TABLE table_name DROP INDEX index_name;
参数解释
| 参数 | 描述 |
|---|---|
| index_name | 索引名称。 |
| table_name | 表名称。 |
示例
通过
DROP INDEX语句删除tbl1的索引。obclient [test]> SHOW INDEX FROM tbl1; +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ | tbl1 | 0 | PRIMARY | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES | | tbl1 | 1 | tbl1_idx1 | 1 | date | A | NULL | NULL | NULL | YES | BTREE | available | | YES | +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ 2 rows in set obclient [test]> DROP INDEX tbl1_idx1 ON tbl1; Query OK, 0 rows affected obclient [test]> SHOW INDEX FROM tbl1; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ | tbl1 | 0 | PRIMARY | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ 1 row in set通过
ALTER TABLE语句删除tbl1_g的索引。obclient [test]> SHOW INDEX FROM tbl1_g; +--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | +--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ | tbl1_g | 1 | tbl1_g_idx1 | 1 | g | A | NULL | NULL | NULL | | SPATIAL | available | | YES | +--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+ 1 row in set obclient [test]> ALTER TABLE tbl1_g DROP INDEX tbl1_g_idx1; Query OK, 0 rows affected obclient [test]> SHOW INDEX FROM tbl1_g; Empty set