O recurso de índice secundário permite consultar dados com base na chave primária de uma tabela de dados e nas colunas de índice do índice secundário criado para essa tabela. Se você precisar usar as colunas de atributo de uma tabela de dados para consultas, crie um índice secundário para acelerar o processo. Ao criar o índice secundário, defina as colunas de índice ou as colunas de atributo como as colunas predefinidas especificadas durante a criação da tabela de dados. Após a criação, use o índice secundário para consultar dados.
Os índices secundários dividem-se em globais e locais. Para mais detalhes sobre esse recurso, consulte Índice secundário.
Crie uma ou mais tabelas de índice ao criar uma tabela de dados por meio da operação CreateTable. Para mais informações, consulte Criar uma tabela de dados.
Pré-requisitos
Instância OTSClient inicializada. Para mais detalhes, consulte Inicializar um cliente Tablestore.
-
Tabela de dados criada com o parâmetro max_versions definido como 1. O parâmetro time_to_live da tabela deve atender a uma das seguintes condições:
Parâmetro time_to_live definido como -1, indicando que os dados na tabela nunca expiram.
Parâmetro time_to_live com valor diferente de -1 e operações de atualização na tabela proibidas.
Colunas predefinidas especificadas para a tabela de dados.
Observações de uso
O nome da tabela de índice deve ser diferente dos nomes de tabelas de séries temporais ou de dados existentes.
Ao criar um índice secundário, o Tablestore adiciona automaticamente as colunas de chave primária da tabela de dados não especificadas como colunas de índice ao índice secundário, funcionando como suas colunas de chave primária.
Para índices secundários locais, a primeira coluna de chave primária da tabela de índice deve ser idêntica à primeira coluna de chave primária da tabela de dados.
Operação de API
/**
* Create a secondary index.
* @api
*
* @param [] $request
* The request parameter, which is the name of the data table.
* @return [] The response.
* @throws OTSClientException The exception that is returned when a parameter error occurs or the Tablestore server returns a verification error.
* @throws OTSServerException The exception that is returned when the Tablestore server returns an error.
* @example "src/examples/CreateIndex.php"
*/
public function createIndex(array $request)
Parâmetros
|
Parâmetro |
Descrição |
|
table_name |
Nome da tabela de dados. |
|
index_meta |
Informações de esquema da tabela de índice, contendo os seguintes itens:
|
|
include_base_data |
Define se os dados existentes da tabela de dados devem ser incluídos na tabela de índice. Valor padrão: false.
|
Exemplos
Criar um índice secundário global
A criação de um índice secundário sem especificar os parâmetros index_type e index_update_mode gera um índice secundário global.
$request = array(
'table_name' => '<TABLE_NAME>', // Specify the name of the data table.
//'include_base_data' => true, // Set the include_base_data parameter to true to include the existing data of the data table to the index table.
'index_meta' => array(
'name' => '<INDEX_NAME>', // Specify the name of the index table.
'primary_key' => array('Col1'), // Specify the primary key column of the index table.
'defined_column' => array('Col2') // Specify the attribute column of the index table.
)
);
$otsClient->createIndex($request);
Também é possível especificar os parâmetros index_type e index_update_mode para criar um índice secundário global.
$request = array(
'table_name' => '<TABLE_NAME>', // Specify the name of the data table.
//'include_base_data' => true, // Set the include_base_data parameter to true to include the existing data of the data table to the index table.
'index_meta' => array(
'name' => '<INDEX_NAME>', // Specify the name of the index table.
'primary_key' => array('Col1'), // Specify the primary key column of the index table.
'defined_column' => array('Col2') // Specify the attribute column of the index table.
'index_type' => IndexTypeConst::GLOBAL_INDEX,
'index_update_mode' => IndexUpdateModeConst::ASYNC_INDEX
)
);
$otsClient->createIndex($request);
Criar um índice secundário local
O exemplo abaixo demonstra como criar um índice secundário local:
$createLocalRequest = array (
'table_name' => '<TABLE_NAME>', // Specify the name of the data table.
//'include_base_data' => true, // Set the include_base_data parameter to true to include the existing data of the data table to the index table.
'index_meta' => array (
'name' => '<INDEX_NAME>', // Specify the name of the index table.
'primary_key' => array('PK0', 'Col1'), // Specify the primary key column of the index table. The first primary key column of the index table must be the same as the first primary key column of the data table.
'defined_column' => array('Col2') // Specify the attribute column of the index table.
'index_type' => IndexTypeConst::LOCAL_INDEX,
'index_update_mode' => IndexUpdateModeConst::SYNC_INDEX
)
);
$otsClient->createIndex($createLocalRequest);
Referências
Após criar um índice secundário, use-o para ler uma única linha de dados ou dados cujos valores de chave primária estejam dentro de um intervalo específico. Para mais informações, consulte Usar um índice secundário para ler dados.
Exclua índices secundários que não são mais necessários. Para mais detalhes, consulte Excluir um índice secundário.