阅读786 返回首页    go 阿里云


CREATE INDEX__数据定义语言_SQL语法参考_云数据库 OceanBase-阿里云

索引是创建在表上的,对数据库表中一列或多列的值进行排序的一种结构。其作用主要在于提高查询的速度,降低数据库系统的性能开销。

OceanBase中,新创建的索引需要等待一次每日合并后,才生效。

格式

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 | SNAPPY1.0 | ZLIB_1.0}'
    | BLOCK_SIZE [=] size
    | STORING(columname_list)
    | index_using_algorithm

columname_list: 
    colname [, colname…]

index_col_name: 
     colname [ASC | DESC] 
  • index_col_name中,每个列名后都支持ASC(升序)和DESC(降序)。默认就为升序。

  • 本语句建立索引的排列方式为:首先以index_col_name中第一个列的值排序;该列值相同的记录,按下一列名的值排序;以此类推。

  • 执行“SHOW INDEX FROM tblname”可以查看创建的索引。

  • “index_option”中,可以指定GLOBAL,LOCAL关键字,表述全局或局部索引。默认是GLOBAL index. 创建带有Parition的表时index一定要加LOCAL关键字。如果没有加,系统将报错。多个index option以空格分隔。

  • 使用可选字段STORING,表示索引表中冗余存储某些列,以提高系统查询系统。(为OceanBase数据库特有

说明: OceanBase内部数据以b树为索引。

举例

Oceanbase>CREATE TABLE test (c1 int primary key, c2 varchar(10));
Query OK, 0 rows affected (0.10 sec)

Oceanbase>create index test_index on test(c1, c2 desc) using hash;
Query OK, 0 rows affected (0.04 sec)

Oceanbase>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 |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-------------+---------------+
| test  |          0 | PRIMARY    |            1 | c1          | A         |        NULL | NULL     | NULL   |      | BTREE      | available   |               |
| test  |          1 | test_index |            1 | c1          | A         |        NULL | NULL     | NULL   |      | BTREE      | unavailable |               |
| test  |          1 | test_index |            2 | c2          | A         |        NULL | NULL     | NULL   | YES  | BTREE      | unavailable |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-------------+---------------+
3 rows in set (0.00 sec)

最后更新:2016-11-24 11:23:47

  上一篇:go RENAME TABLE__数据定义语言_SQL语法参考_云数据库 OceanBase-阿里云
  下一篇:go DROP INDEX__数据定义语言_SQL语法参考_云数据库 OceanBase-阿里云