---
title: "关于表的索引 | OceanBase 文档中心"
description: 关于表的索引 可以在表的一个或多个列上创建索引以加速表上的 SQL 语句执行速度。索引使用正确的话，可以减少物理 IO 或者逻辑 IO。 如果创建表时同时设置了主键，OceanBase 数据库会默认创建一个唯一索引。以下面的 SQL 为例： obclient&gt; DROP TABLE IF EXISTS t1; Que…
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*P8CuR4UJ_FkAAAAAAAAAAAAADiGDAQ/original) OceanBase 数据库分布式版 - V 3.2.1 企业版

# 关于表的索引

更新时间：2025-01-17 15:21:36

可以在表的一个或多个列上创建索引以加速表上的 SQL 语句执行速度。索引使用正确的话，可以减少物理 IO 或者逻辑 IO。

如果创建表时同时设置了主键，OceanBase 数据库会默认创建一个唯一索引。以下面的 SQL 为例：

```javascript
obclient> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected (0.01 sec)

obclient> CREATE TABLE t1(id bigint not null primary key, name varchar(50));
Query OK, 0 rows affected (0.05 sec)

obclient> show indexes from t1\G
*************************** 1. row ***************************
        Table: t1
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: NULL
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment: available
Index_comment:
      Visible: YES
1 row in set (0.01 sec)

```

## 新增索引

为表增加索引可以通过 CREATE INDEX 语句。OceanBase 能在普通表和分区表上创建索引，索引可以是本地索引或者全局索引。同时索引可以是唯一索引或者普通索引，如果是分区表的唯一索引，唯一索引必须包含表分区的拆分键。

创建索引的 SQL 语法格式如下：

```javascript
CREATE [UNIQUE] INDEX index_name ON table_name ( column_list ) [LOCAL | GLOBAL] [ PARTITION BY column_list PARTITIONS N ] ;

```

MySQL 租户里，索引名称在数据库（DataBase）范围内不能重复，查看索引可以通过命令 SHOW INDEXES 。

在 MySQL 租户里，新增索引还有一种方法，SQL 语法格式如下：

```javascript
ALTER TABLE  table_name  
ADD|DROP  INDEX|KEY  index_name ( column_list ) ;

```

可以一次增加多个索引，索引关键字用 INDEX 或 KEY 都可以。

示例：对分区表新增索引

```javascript
obclient> create table t3(
    id bigint not null primary KEY
    , name varchar(50)
    , gmt_create timestamp not null default current_timestamp
) partition by hash(id) partitions 8;
Query OK, 0 rows affected (0.14 sec)

obclient> alter table t3 add unique key t3_uk (name) local;
ERROR 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function

obclient> alter table t3
    add unique key t3_uk (name, id) LOCAL
    , add key t3_ind3(gmt_create) global;
Query OK, 0 rows affected (18.03 sec)

obclient> show indexes from t3;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment   | Index_comment | Visible |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| t3    |          0 | PRIMARY  |            1 | id          | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
| t3    |          0 | t3_uk    |            1 | name        | A         |        NULL | NULL     | NULL   | YES  | BTREE      | available |               | YES     |
| t3    |          0 | t3_uk    |            2 | id          | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
| t3    |          1 | t3_ind3  |            1 | gmt_create  | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
4 rows in set (0.01 sec)

```

## 删除索引

删除索引的语法格式如下：

```javascript
ALTER TABLE table_name DROP key|index index_name ;

```

示例：删除表的索引

```javascript
obclient> alter table t3 drop key t3_uk, drop key t3_ind3;
Query OK, 0 rows affected (0.07 sec)

```

 上一篇 下一篇 ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
