二级索引相当于把数据表的主键查询能力扩展到了不同的列,当需要使用属性查询数据时,您可以通过创建二级索引加快数据查询的效率。设置预定义列后,在创建二级索引时将预定义列作为索引表的索引列或者属性列。
前提条件
已初始化Client。具体操作,请参见初始化Tablestore Client。
已创建数据表,且数据表的最大版本数(MaxVersions)必须为1,数据生命周期(TimeToLive)必须满足如下条件中的任意一个。
数据表的数据生命周期为-1(数据永不过期)。
数据表的数据生命周期不为-1时,数据表为禁止更新状态。
数据表已设置预定义列。
功能说明
二级索引通过重新组合数据表的主键列和预定义列,为数据表提供不同于原主键的查询维度。二级索引分为全局二级索引和本地二级索引,两种索引的数据同步方式和主键要求不同。更多信息,请参见二级索引。
调用 createIndex 为已有数据表创建二级索引。如果需要在创建数据表时同时创建一个或多个二级索引,可在 createTable 请求中配置索引结构。具体操作,请参见创建数据表。
public CreateIndexResponse createIndex(CreateIndexRequest createIndexRequest)
throws TableStoreException, ClientException
以下示例为 example_table 创建全局二级索引 example_global_index。索引表包含存量数据。
IndexMeta indexMeta = new IndexMeta("example_global_index");
indexMeta.addPrimaryKeyColumn("category");
indexMeta.addDefinedColumn("status");
CreateIndexRequest request =
new CreateIndexRequest("example_table", indexMeta, true);
client.createIndex(request);
参数
参数 | 说明 |
MainTableName | 数据表名称。 |
IndexMeta | 索引表的结构信息,包括如下内容:
|
IncludeBaseData | 索引表中是否包含数据表中已存在的数据。 当设置IncludeBaseData为true时,表示包含存量数据;设置IncludeBaseData为false时,表示不包含存量数据。 |
示例
创建全局二级索引
以下示例用于在主键为pk1、pk2的数据表上创建主键列为definedcol1,属性列为definedcol2的索引表。该索引为全局二级索引且包括存量数据。该索引表的完整主键为definedcol1、pk1、pk2,属性列为definedcol2。
func CreateGlobalIndexSample(client *tablestore.TableStoreClient, tableName string) {
//新建索引表Meta。
indexMeta := new(tablestore.IndexMeta)
//设置数据表的definedcol1列作为索引表的主键。
indexMeta.AddPrimaryKeyColumn("definedcol1")
//设置数据表的definedcol2列作为索引表的属性列。
indexMeta.AddDefinedColumn("definedcol2")
//设置索引表名称。
indexMeta.IndexName = "<INDEX_NAME>"
indexReq := &tablestore.CreateIndexRequest{
//添加索引表到数据表。
MainTableName:tableName,
IndexMeta: indexMeta,
/**
通过将IncludeBaseData参数设置为true,创建索引表后会开启数据表中存量数据的同步,然后可以通过索引表查询全部数据,
同步时间跟数据量的大小有一定的关系。
*/
IncludeBaseData: true,
}
resp, err := client.CreateIndex(indexReq)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create index finished", resp)
}
}创建本地二级索引
以下示例用于在主键为pk1、pk2的数据表上创建主键列为pk1、definedcol1,属性列为definedcol2的索引表。该索引为本地二级索引且包括存量数据。该索引表的完整主键为pk1、definedcol1、pk2,属性列为definedcol2。
func CreateGLocalIndexSample(client *tablestore.TableStoreClient, tableName string) {
//新建索引表Meta。
indexMeta := new(tablestore.IndexMeta)
//索引表的第一列主键必须与数据表的第一列主键相同。
indexMeta.AddPrimaryKeyColumn("pk1")
//设置数据表的definedcol1列作为索引表的主键。
indexMeta.AddPrimaryKeyColumn("definedcol1")
//设置数据表的definedcol2列作为索引表的属性列。
indexMeta.AddDefinedColumn("definedcol2")
//设置索引类型为本地二级索引(IT_LOCAL_INDEX)。
indexMeta.IndexType = tablestore.IT_LOCAL_INDEX
//设置索引表名称。
indexMeta.IndexName = "<INDEX_NAME>"
indexReq := &tablestore.CreateIndexRequest{
//添加索引表到数据表。
MainTableName:tableName,
IndexMeta: indexMeta,
/**
通过将IncludeBaseData参数设置为true,创建索引表后会开启数据表中存量数据的同步,然后可以通过索引表查询全部数据,
同步时间跟数据量的大小有一定的关系。
*/
IncludeBaseData: true,
}
resp, err := client.CreateIndex(indexReq)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create index finished", resp)
}
}相关文档
创建二级索引后,您可以单行读取或者范围读取数据。具体操作,请参见使用二级索引读取数据。
如果不再需要使用二级索引,您可以删除二级索引。具体操作,请参见删除二级索引。