You can execute the show index statement to query the index information about a table, such the index name, index fields, and index type.
For more information about the show index statement, see Query the index information about a table.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A mapping table is created. For more information, see Create mapping tables for tables.
Parameters
Parameter | Description |
query | The SQL statement. Configure the parameter based on the required feature. |
Examples
The following sample code provides an example on how to run the show index in test_table
statement to query the index information about the table named test_table:
func showIndex(client *tablestore.TableStoreClient) {
// Create an SQL request.
request := &tablestore.SQLQueryRequest{Query: "show index in test_table"}
// Obtain the response to the SQL request.
response, err := client.SQLQuery(request)
if err != nil {
panic(err)
}
// Obtain the schema of the returned results of the SQL request.
columns := response.ResultSet.Columns()
fmt.Printf("response table schema:[")
for l := 0; l < len(columns); l++ {
fmt.Printf("%v:%v ", columns[l].Name, columns[l].Type.String())
}
// Use SQL ResultSet to obtain all returned results of the SQL request.
fmt.Println("]\nresponse resultset:")
resultSet := response.ResultSet
for resultSet.HasNext() {
row := resultSet.Next()
tableName, _ := row.GetStringByName("Table")
fmt.Printf("%v, ", tableName)
nonUnique, _ := row.GetInt64ByName("Non_unique")
fmt.Printf("%v, ", nonUnique)
keyName, _ := row.GetStringByName("Key_name")
fmt.Printf("%v, ", keyName)
seqInIndex, _ := row.GetInt64ByName("Seq_in_index")
fmt.Printf("%v, ", seqInIndex)
columnName, _ := row.GetStringByName("Column_name")
fmt.Printf("%v, ", columnName)
indexType, _ := row.GetStringByName("Index_type")
fmt.Printf("%v\n", indexType)
}
}
Sample response:
response table schema: [Table:STRING Non_unique:INTEGER Key_name:STRING Seq_in_index:INTEGER Column_name:STRING Is_defined_column:STRING Search_type:STRING Collation:STRING Cardinality:INTEGER Sub_part:INTEGER Packed:STRING Null:STRING Index_type:STRING Comment:STRING Index_comment:STRING Visible:STRING Expression:STRING]
response resultset:
test_table, 0, PRIMARY, 1, pk,
test_table, 1, test_table_index, 1, pk, SearchIndex
test_table, 1, test_table_index, 2, bool_value, SearchIndex
test_table, 1, test_table_index, 3, double_value, SearchIndex
test_table, 1, test_table_index, 4, long_value, SearchIndex
test_table, 1, test_table_index, 5, string_value, SearchIndex
References
If you want to use a specific search index to query data when you use the SQL query feature, you can execute the
CREATE TABLE
statement to create a mapping table for the search index. For more information, see Create mapping tables for search indexes.You can execute SQL statements to query data based on the fields of an index. For more information, see Query data.