CREATE INDEX(AUTO模式語(yǔ)法)
PolarDB-X支持創(chuàng)建局部索引和全局二級(jí)索引 (Global Secondary Index,GSI) ,同時(shí)支持刪除這兩種索引。本語(yǔ)法僅適用于AUTO模式數(shù)據(jù)庫(kù)。
局部索引
關(guān)于局部索引,詳情請(qǐng)參見(jiàn)CREATE INDEX Statement。
注意事項(xiàng)
若全局索引要使用二級(jí)分區(qū)的相關(guān)功能,實(shí)例版本必須為5.4.17-16952556及以上。
全局二級(jí)索引
關(guān)于全局二級(jí)索引基本原理,請(qǐng)參見(jiàn)全局二級(jí)索引。
語(yǔ)法
CREATE [UNIQUE]
GLOBAL INDEX index_name [index_type]
ON tbl_name (index_sharding_col_name,...)
global_secondary_index_option
[index_option]
[algorithm_option | lock_option] ...
# 全局二級(jí)索引特有語(yǔ)法,具體說(shuō)明請(qǐng)參見(jiàn)CREATE TABLE文檔
global_secondary_index_option:
[COVERING (col_name,...)]
[partition_options]
[VISIBLE|INVISIBLE]
# 分區(qū)定義
partition_options:
partition_columns_definition
[subpartition_columns_definition]
[subpartition_specs_definition]/*用于定義模板化二級(jí)分區(qū)*/
partition_specs_definition
# 一級(jí)分區(qū)的分區(qū)列定義
partition_columns_definition:
PARTITION BY
HASH({column_name | partition_func(column_name)}) partitions_count
| KEY(column_list) partitions_count
| RANGE({column_name | partition_func(column_name)})
| RANGE COLUMNS(column_list)
| LIST({column_name | partition_func(column_name)})
| LIST COLUMNS(column_list)
# 二級(jí)分區(qū)的分區(qū)列定義
subpartition_columns_definition:
SUBPARTITION BY
HASH({column_name | partition_func(column_name)}) subpartitions_count
| KEY(column_list) subpartitions_count
| RANGE({column_name | partition_func(column_name)})
| RANGE COLUMNS(column_list)
| LIST({column_name | partition_func(column_name)})
| LIST COLUMNS(column_list)
partitions_count:
PARTITIONS partition_count
subpartitions_count:
SUBPARTITIONS partition_count
# 分區(qū)函數(shù)定義
partition_func:
YEAR
| TO_DAYS
| TO_MONTHS
| TO_WEEKS
| TO_SECOND
| UNIX_TIMESTAMP
| MONTH
| DAYOFWEEK
| DAYOFMONTH
| DAYOFYEAR
| SUBSTR
| SUBSTRING
# 一級(jí)分區(qū)的三種分區(qū)類(lèi)型定義
partition_specs_definition:
hash_partition_list
| range_partition_list
| list_partition_list
# 二級(jí)分區(qū)的三種分區(qū)類(lèi)型定義
subpartition_specs_definition:
hash_subpartition_list
| range_subpartition_list
| list_subpartition_list
# 一級(jí)分區(qū)的Hash/Key分區(qū)定義
hash_partition_list:
/*hash允許不指定各個(gè)具體分區(qū)定義*/
| ( hash_partition [, hash_partition, ...] )
hash_partition:
PARTITION partition_name [partition_spec_options] /*適用于純一級(jí)分區(qū)或使用模板化子分區(qū)*/
| PARTITION partition_name subpartitions_count [subpartition_specs_definition] /*適用于定義一級(jí)分區(qū)下的非模板化子分區(qū)*/
# 二級(jí)級(jí)分區(qū)的Hash/Key分區(qū)定義
hash_subpartition_list:
| empty
| ( hash_subpartition [, hash_subpartition, ...] )
hash_subpartition:
SUBPARTITION subpartition_name [partition_spec_options]
# 一級(jí)分區(qū)的Range/Range Columns分區(qū)定義
range_partition_list:
( range_partition [, range_partition, ... ] )
range_partition:
PARTITION partition_name VALUES LESS THAN (range_bound_value) [partition_spec_options] /*適用于純一級(jí)分區(qū)或使用模板化子分區(qū)*/
| PARTITION partition_name VALUES LESS THAN (range_bound_value) [[subpartitions_count] [subpartition_specs_definition]] /*適用于定義一級(jí)分區(qū)下的非模板化子分區(qū)*/
# 二級(jí)分區(qū)的Range/Range Columns分區(qū)定義
range_subpartition_list:
( range_subpartition [, range_subpartition, ... ] )
range_subpartition:
SUBPARTITION subpartition_name VALUES LESS THAN (range_bound_value) [partition_spec_options]
range_bound_value:
maxvalue /*適用于定義range的maxvalue分區(qū)*/
| expr /*適用于使用單個(gè)分區(qū)列時(shí)range邊界值*/
| value_list /*適用于使用多個(gè)分區(qū)列時(shí)range邊界值*/
# 一級(jí)分區(qū)的List/List Columns分區(qū)定義
list_partition_list:
(list_partition [, list_partition ...])
list_partition:
PARTITION partition_name VALUES IN (list_bound_value) [partition_spec_options] /*適用于純一級(jí)分區(qū)或使用模板化子分區(qū)*/
| PARTITION partition_name VALUES IN (list_bound_value) [[subpartitions_count] [subpartition_specs_definition]] /*適用于定義一級(jí)分區(qū)下的非模板化子分區(qū)*/
# 二級(jí)分區(qū)的List/List Columns分區(qū)定義
list_subpartition_list:
(list_subpartition [, list_subpartition ...])
list_subpartition:
SUBPARTITION subpartition_name VALUES IN (list_bound_value) [partition_spec_options]
list_bound_value:
default /*適用于定義list的default分區(qū)*/
| value_set
value_set:
value_list /*適用于使用單個(gè)分區(qū)列時(shí)values集合*/
| (value_list) [, (value_list), ...] /*適用于使用多個(gè)分區(qū)列時(shí)的values集合*/
value_list:
value [, value, ...]
partition_spec_options:
[[STORAGE] ENGINE [=] engine_name]
[COMMENT [=] 'string']
[LOCALITY [=] locality_option]
table_option:
[[STORAGE] ENGINE [=] engine_name]
[COMMENT [=] 'string']
[{CHARSET | CHARACTER SET} [=] charset]
[COLLATE [=] collation]
[TABLEGROUP [=] table_group_id]
[LOCALITY [=] locality_option]
locality_option:
'dn=storage_inst_id_list'
storage_inst_id_list:
storage_inst_id[,storage_inst_id_list]
local_partition_definition:
LOCAL PARTITION BY RANGE (column_name)
[STARTWITH 'yyyy-MM-dd']
INTERVAL interval_count [YEAR|MONTH|DAY]
[EXPIRE AFTER expire_after_count]
[PRE ALLOCATE pre_allocate_count]
[PIVOTDATE pivotdate_func]
[DISABLE SCHEDULE]
pivotdate_func:
NOW()
| DATE_ADD(...)
| DATE_SUB(...)
CREATE GLOBAL INDEX
系列語(yǔ)法用于在建表后添加GSI,該系列語(yǔ)法在MySQL語(yǔ)法上新引入了GLOBAL關(guān)鍵字,用于指定添加的索引類(lèi)型為GSI。目前建表后創(chuàng)建GSI存在一定限制,關(guān)于GSI的限制與約定,詳情請(qǐng)參見(jiàn)如何使用全局二級(jí)索引。
關(guān)于全局二級(jí)索引定義子句的詳細(xì)說(shuō)明,請(qǐng)參見(jiàn)CREATE TABLE(DRDS模式)。
示例
下面以建立普通全局二級(jí)索引為例,介紹如何在建表后創(chuàng)建GSI。
# 先創(chuàng)建表
CREATE TABLE t_order (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`order_id` varchar(20) DEFAULT NULL,
`buyer_id` varchar(20) DEFAULT NULL,
`seller_id` varchar(20) DEFAULT NULL,
`order_snapshot` longtext DEFAULT NULL,
`order_detail` longtext DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `l_i_order` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 partition by hash(`order_id`) partitions 16;
# 再創(chuàng)建全局二級(jí)索引
CREATE GLOBAL INDEX `g_i_seller` ON t_order (`seller_id`) partition by hash(`seller_id`) partitions 16;
主表:”t_order“是分區(qū)表,分區(qū)的拆分方式為按照”order_id“列進(jìn)行哈希。
索引表:”g_i_seller“按照”seller_id“列進(jìn)行哈希,指定覆蓋列為”order_snapshot“。
索引定義子句:
GLOBAL INDEX `g_i_seller` ON t_order (`seller_id`) partition by hash(`seller_id`) partitions 16
。
通過(guò)SHOW INDEX
查看索引信息,包含拆分鍵order_id上的局部索引,seller_id、id和order_id上的GSI,其中seller_id為索引表的拆分鍵,id和order_id為默認(rèn)的覆蓋列(主鍵和主表的拆分鍵)。
關(guān)于GSI的限制與約定,詳情請(qǐng)參見(jiàn)如何使用全局二級(jí)索引,SHOW INDEX詳細(xì)說(shuō)明,請(qǐng)參見(jiàn)SHOW INDEX。
show index from t_order;
返回信息如下:
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t_order_****_00000 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| t_order_****_00000 | 1 | l_i_order | 1 | order_id | A | 0 | NULL | NULL | YES | BTREE | | |
+--------------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.02 sec)
通過(guò)SHOW GLOBAL INDEX
可以單獨(dú)查看GSI信息,詳情請(qǐng)參見(jiàn)SHOW GLOBAL INDEX。
show global index from t_order;
+---------------------+---------+------------+------------+-------------+----------------+------------+------------------+---------------------+--------------------+------------------+---------------------+--------------------+--------+
| SCHEMA | TABLE | NON_UNIQUE | KEY_NAME | INDEX_NAMES | COVERING_NAMES | INDEX_TYPE | DB_PARTITION_KEY | DB_PARTITION_POLICY | DB_PARTITION_COUNT | TB_PARTITION_KEY | TB_PARTITION_POLICY | TB_PARTITION_COUNT | STATUS |
+---------------------+---------+------------+------------+-------------+----------------+------------+------------------+---------------------+--------------------+------------------+---------------------+--------------------+--------+
| ZZY3_DRDS_LOCAL_APP | t_order | 1 | g_i_seller | seller_id | id, order_id | NULL | seller_id | HASH | 4 | | NULL | NULL | PUBLIC |
+---------------------+---------+------------+------------+-------------+----------------+------------+------------------+---------------------+--------------------+------------------+---------------------+--------------------+--------+
查看索引表的結(jié)構(gòu),索引表包含主表的主鍵、分庫(kù)分表鍵、默認(rèn)的覆蓋列和自定義覆蓋列,主鍵列去除了AUTO_INCREMENT
屬性,并且去除了主表中的局部索引。
show create table g_i_seller;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| g_i_seller | CREATE TABLE `g_i_seller` (
`id` bigint(11) NOT NULL,
`order_id` varchar(20) DEFAULT NULL,
`seller_id` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `auto_shard_key_seller_id` (`seller_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 partition by hash(`seller_id`) partitions 16 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+