创建二级索引。
Lindorm提供了主键索引功能,即按照Primary Key的定义进行排序的索引。Lindorm SELECT可基于此主键索引高效地执行整行查询等操作。但若需要使用主键(Primary Key)之外的列进行查询,可以通过Lindorm提供的二级索引来满足上述需求。
语法
create_index_statement ::= CREATE INDEX [ index_name ]
ON table_name '(' index_identifier ')'
[INCLUDE include_identifier]
[ASYNC]
[ with '(' index_options ')' ]
index_identifier ::= '('column_name1 [desc],...,column_namen [desc]')'
include_identifier ::= '('column_name1,...,column_namen ')'
支持的Index_Options列表
名称 | 类型 | 描述 |
---|---|---|
COMPRESSION | String | 索引表的压缩算法,支持的压缩算法包括:
|
INDEX_COVERED_TYPE | String | 表示索引的冗余方式,支持如下方式:
|
STARTKEY | String | 表示索引表的起始Key。 |
ENDKEY | String | 表示索引表的终止Key。 |
NUMREGIONS | String | 表示索引表的预分区数。 |
说明
- 索引表支持冗余列,冗余列可以帮助加速查询,避免回查主表。如果只是冗余某几个列可以使用Include。Include(c1,c2)表示冗余c1和c2列。
- 默认表的索引构建方式是同步的,但是如果添加Async表示异步构建,需要执行BUILD INDEX命令。
- 索引表数目不超过5个,数目过多会增加成本和写入RT。
示例
//异步建索引,执行后返回,后台不对历史数据建索引,如需构建,使用BUILD INDEX命令。
create index idx1 on test(c4 desc) include(c5,c6) async with (COMPRESSION='ZSTD');
//同步建索引,执行后阻塞,对历史数据建完索引后返回。
create index idx1 on test(c4 desc) include(c5,c6) with ( COMPRESSION ='ZSTD');
//冗余所有列。
create index idx1 on test(c4 desc) with (INDEX_COVERED_TYPE ='COVERED_ALL_COLUMNS_IN_SCHEMA');
//冗余动态列。
create index idx1 on test(c4 desc) with (INDEX_COVERED_TYPE='COVERED_DYNAMIC_COLUMNS');
//如果要设置预分区,可根据需要指定STARTKEY, ENDKEY和NUMREGIONS三个参数。
//不指定STARTKEY和ENDKEY参数。表示在全表范围内分成32个预分区。
create index idx1 on test(c4 desc) include(c5,c6) with (NUMREGIONS ='32');
//指定STARTKEY和ENDKEY参数。表示在11111111和9999999之间分成32个预分区。
create index idx1 on test(c4 desc) include(c5,c6) with (NUMREGIONS ='32', STARTKEY ='11111111', ENDKEY = '9999999');