天天看點

開發指南—透明分布式—聚簇索引

前提條件

PolarDB-X核心小版本需為5.4.9或以上。

注意事項

  • 聚簇索引是一種特殊的全局二級索引,相關行為和限制請參考 全局二級索引
  • 聚簇索引的覆寫列預設包含主表的所有列,并在主表的列發生變更時,自動同步修改聚簇索引表,保證聚簇索引表和主表的實時同步。
  • 聚簇索引表也會和主表的本地索引保持同步。

文法

您可以在建表或加索引的語句中,通過

CLUSTERED

關鍵字指定建立的索引為聚簇索引。

  • CREATE TABLE:
CREATE [SHADOW] TABLE [IF NOT EXISTS] tbl_name
    (create_definition, ...)
    [table_options]
    [drds_partition_options]
create_definition:
    [UNIQUE] CLUSTERED INDEX index_name [index_type] (index_col_name,...)
      [drds_partition_options] 
      [index_option] ...      
  • 說明 僅在主鍵拆分表中可省略拆分規則即

    [drds_partition_options]

    部分。
  • CREATE INDEX:
CREATE [UNIQUE]
    CLUSTERED INDEX index_name [index_type]
    ON tbl_name (index_col_name,...)
    [drds_partition_options]
    [index_option] ...      
  • [drds_partition_options]

  • ALTER TABLE:
ALTER TABLE tbl_name
    alter_specification      
  • 其中

    alter_specification

    支援如下規則:
alter_specification:
  | ADD [UNIQUE] CLUSTERED {INDEX|KEY} index_name 
      [index_type] (index_col_name,...)
      [drds_partition_options] 
      [index_option] ...      
  • 說明
    • 聚簇索引相關變更(即

      alter_specification

      部分)僅支援使用一條變更規則。
    • 聚簇索引必須顯式指定索引名。
    • 僅在主鍵拆分表中可省略拆分規則(即

      [drds_partition_options]

      部分)。

使用示例

假設已使用如下語句在PolarDB-X資料庫中建立了一張

t_order

表:

CREATE PARTITION TABLE `t_order` (
    ->   `t` timestamp null default CURRENT_TIMESTAMP,
    ->   `x` int default 3,
    ->   `order_id` varchar(20) DEFAULT NULL,
    ->   `seller_id` varchar(20) DEFAULT NULL
    -> );      

您可以使用如下語句為

t_order

表添加聚簇索引:

CREATE CLUSTERED INDEX `c_i` ON `t_order` (seller_id, x)      

添加成功後,您可以使用如下語句檢視主表結構,來确認聚簇索引的定義:

SHOW CREATE TABLE t_order;      

傳回結果如下:

+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                                                                                                                                                 |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_order | CREATE PARTITION TABLE `t_order` (
  `t` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `x` int(11) DEFAULT '3',
  `order_id` varchar(20) DEFAULT NULL,
  `seller_id` varchar(20) DEFAULT NULL,
  LOCAL KEY `_local_c_i` (`seller_id`, `x`),
  CLUSTERED INDEX `c_i`(`seller_id`, `x`) DBPARTITION BY HASH(`seller_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4   |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.08 sec)      

您還可以通過如下語句檢視聚簇索引表結構:

SHOW CREATE TABLE c_i;      

從如下傳回結果中,可以看到聚簇索引表包含了主表所有的列:

+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                            |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| c_i   | CREATE TABLE `c_i` (
  `t` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `x` int(11) DEFAULT '3',
  `order_id` varchar(20) DEFAULT NULL,
  `seller_id` varchar(20) DEFAULT NULL,
  KEY `auto_shard_key_seller_id` USING BTREE (`seller_id`),
  KEY `i_seller_id_x` USING BTREE (`seller_id`, `x`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4  dbpartition by hash(`seller_id`) |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.03 sec)