129 lines
4.0 KiB
Markdown
129 lines
4.0 KiB
Markdown
关于表的索引
|
|
===========================
|
|
|
|
|
|
|
|
可以在表的一个或多个列上创建索引以加速表上的 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 租户里,索引名称在表范围内不能重复,查看索引可以通过命令 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)
|
|
```
|
|
|
|
|