OceanBase CREATE INDEX

2021-06-10 15:20 更新

描述

該語句用來創(chuàng)建索引。索引是創(chuàng)建在表上的,對(duì)數(shù)據(jù)庫表中一列或多列的值進(jìn)行排序的一種結(jié)構(gòu)。其作用主要在于提高查詢的速度,降低數(shù)據(jù)庫系統(tǒng)的性能開銷。

格式

CREATE [UNIQUE] INDEX indexname 
?????ON tblname (index_col_name,...) 
      [index_type] [index_options] 
index_type: 
      USING BTREE

index_options: 
      index_option [index_option…]
      
index_option: 
    GLOBAL | LOCAL
    | COMMENT 'string'
    | COMPRESSION [=] {NONE | LZ4_1.0 | LZO_1.0 | SNAPPY_1.0 | ZLIB_1.0}
    | BLOCK_SIZE [=] size
    | STORING(columname_list) 
    | VISIBLE | INVISIBLE

index_col_name: 
    colname [(length)] [ASC | DESC]

columname_list: 
    colname [, colname…]

參數(shù)解釋

參數(shù)

描述

indexname

指定要?jiǎng)?chuàng)建的索引名稱。

tblname

指過索引所屬的表名。

index_col_name

指定索引的列名,每個(gè)列名后都支持 ASC(升序),不支持 DESC(降序)。默認(rèn)為升序。

建立索引的排序方式為:首先以 index_col_name 中第一個(gè)列的值排序;該列值相同的記錄,按下一列名的值排序;以此類推。

index_type

索引類型,只支持 USING BTREE,以 B 樹為索引。

UNIQUE

指定為唯一索引。

index_option

指定索引選項(xiàng),多個(gè) index_option 以空格分隔。

GLOBAL | LOCAL

指定該索引是全局索引或局部索引,默認(rèn)是 GLOBAL。

COMMENT

指定注釋。

COMPRESSION

指定壓縮算法。

BLOCK_SIZE

指定微塊大小。

STORING

表示索引表中冗余存儲(chǔ)某些列,以提高系統(tǒng)查詢性能。

示例

  1. 執(zhí)行以下命令,創(chuàng)建表 test。
obclient> CREATE TABLE test (c1 int primary key, c2 VARCHAR(10));
  1. 執(zhí)行以下命令,創(chuàng)建表 test 的索引。
obclient> CREATE INDEX test_index ON test (c1, c2 ASC);
  1. 執(zhí)行以下命令,查看表 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     |
| test  |          1 | test_index |            1 | c1          | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
| test  |          1 | test_index |            2 | c2          | A         |        NULL | NULL     | NULL   | YES  | BTREE      | available |               | YES     |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
3 rows in set (0.05 sec)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)