PolarDB-X支持創(chuàng)建局部索引和全局二級(jí)索引 (Global Secondary Index, GSI) ,同時(shí)支持刪除這兩種索引。本語法僅適用于DRDS模式數(shù)據(jù)庫。
局部索引
關(guān)于局部索引,詳情請(qǐng)參見CREATE INDEX Statement。
全局二級(jí)索引
關(guān)于全局二級(jí)索引基本原理,請(qǐng)參見全局二級(jí)索引。
語法
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í)索引特有語法,具體說明請(qǐng)參見CREATE TABLE文檔
global_secondary_index_option:
[COVERING (col_name,...)]
drds_partition_options
[VISIBLE|INVISIBLE]
# 分庫分表子句,具體說明請(qǐng)參見CREATE TABLE文檔
drds_partition_options:
DBPARTITION BY db_sharding_algorithm
[TBPARTITION BY {table_sharding_algorithm} [TBPARTITIONS num]]
db_sharding_algorithm:
HASH([col_name])
| {YYYYMM|YYYYWEEK|YYYYDD|YYYYMM_OPT|YYYYWEEK_OPT|YYYYDD_OPT}(col_name)
| UNI_HASH(col_name)
| RIGHT_SHIFT(col_name, n)
| RANGE_HASH(col_name, col_name, n)
table_sharding_algorithm:
HASH(col_name)
| {MM|DD|WEEK|MMDD|YYYYMM|YYYYWEEK|YYYYDD|YYYYMM_OPT|YYYYWEEK_OPT|YYYYDD_OPT}(col_name)
| UNI_HASH(col_name)
| RIGHT_SHIFT(col_name, n)
| RANGE_HASH(col_name, col_name, n)
# 以下為MySQL DDL語法
index_sharding_col_name:
col_name [(length)] [ASC | DESC] # length參數(shù)僅用于在索引表拆分鍵上創(chuàng)建局部索引
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'
index_type:
USING {BTREE | HASH}
algorithm_option:
ALGORITHM [=] {DEFAULT|INPLACE|COPY}
lock_option:
LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE}
CREATE GLOBAL INDEX
系列語法用于在建表后添加GSI,該系列語法在MySQL語法上新引入了GLOBAL關(guān)鍵字,用于指定添加的索引類型為GSI。目前建表后創(chuàng)建GSI存在一定限制,關(guān)于GSI的限制與約定,詳情請(qǐng)參見如何使用全局二級(jí)索引。
關(guān)于全局二級(jí)索引定義子句詳細(xì)說明,請(qǐng)參見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 dbpartition by hash(`order_id`);
# 創(chuàng)建全局二級(jí)索引
ALTER TABLE t_order ADD UNIQUE GLOBAL INDEX `g_i_buyer` (`buyer_id`) COVERING (`order_snapshot`) dbpartition by hash(`buyer_id`);
- 主表:”t_order“只分庫不分表,分庫的拆分方式為按照”order_id“列進(jìn)行哈希。
- 索引表:”g_i_buyer“只分庫不分表,分庫的拆分方式為按照”buyer_id“列進(jìn)行哈希,指定覆蓋列為”order_snapshot“。
- 索引定義子句:
GLOBAL INDEX `g_i_seller` ON t_order (`seller_id`) dbpartition by hash(`seller_id`)
。
通過
SHOW INDEX
查看索引信息,包含拆分鍵order_id上的局部索引,以及seller_id、id和order_id上的GSI,其中seller_id為索引表的拆分鍵,id和order_id為默認(rèn)的覆蓋列(主鍵和主表的拆分鍵)。說明 關(guān)于GSI的限制與約定,詳情請(qǐng)參見如何使用全局二級(jí)索引,SHOW INDEX詳細(xì)說明,請(qǐng)參見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 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| t_order | 1 | l_i_order | 1 | order_id | A | 0 | NULL | NULL | YES | BTREE | | |
| t_order | 1 | g_i_seller | 1 | seller_id | NULL | 0 | NULL | NULL | YES | GLOBAL | INDEX | |
| t_order | 1 | g_i_seller | 2 | id | NULL | 0 | NULL | NULL | | GLOBAL | COVERING | |
| t_order | 1 | g_i_seller | 3 | order_id | NULL | 0 | NULL | NULL | YES | GLOBAL | COVERING | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+----------+---------------+
通過
SHOW GLOBAL INDEX
可以單獨(dú)查看GSI信息,詳情請(qǐng)參見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),索引表包含主表的主鍵、分庫分表鍵、默認(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 dbpartition by hash(`seller_id`) |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+