本文介紹PolarDB-X數據庫使用FORCE INDEX(索引指定)功能的相關限制、注意事項以及使用示例。
背景
PolarDB-X提供了全局索引和局部索引(基于主表和全局索引都可以創建局部索引),并拓展了MySQL生態的FORCE INDEX和INDEX HINT的功能,讓其可以支持兩層索引指定,既可以指定全局索引,也可以指定局部索引。
支持版本
計算節點組件版本低于5.4.18,僅支持一層索引指定。
計算節點組件版本為5.4.18,小版本低于17181576,僅支持一層索引指定;小版本不低于17181576,支持一層索引指定和二層索引指定。
計算節點組件版本為5.4.19,發布日期低于2024-07-10,僅支持一層索引指定;發布日期不低于2024-07-10,支持一層索引指定和二層索引指定。
計算節點組件版本高于5.4.19,支持一層索引指定和二層索引指定。
注意事項
索引名不支持前綴匹配,必須指定索引全名。
使用FORCE INDEX(LOCAL INDEX)時,局部索引生效規則:
包含該局部索引的全局索引會被基于代價進行選擇,只有全局索引被選中時,該局部索引才生效。
不包含該局部索引的全局索引不會被選擇。
使用FORCE INDEX(PRIMARY)時,所有的全局索引都不會被選擇。
語法
一層索引指定
# FORCE INDEX
tbl_name [[AS] alias] [index_hint]
index_hint:
FORCE INDEX({gsi_name|local_index_name|primary})
# INDEX HINT
/*+TDDL: INDEX({table_name|table_alias}, {local_index_name|gsi_name|primary})*/
參數說明
gsi_name:全局索引名
local_index_name:局部索引名
primary:主鍵
二層索引指定
# FORCE INDEX
tbl_name [[AS] alias] [index_hint]
index_hint:
FORCE INDEX(gsi_name[.{local_index_name|primary}] | local_index_name | primary[.{local_index_name|primary}])
# INDEX HINT
/*+TDDL: INDEX({table_name|table_alias}, {local_index_name|gsi_name|primary} [, {local_index_name|primary}])*/
參數說明
gsi_name:全局索引名
local_index_name:局部索引名
primary:主鍵
示例
CREATE TABLE `t_order` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`order_id` varchar(20) NOT NULL,
`buyer_id` varchar(20) DEFAULT NULL,
`seller_id` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
GLOBAL INDEX `g_i_buyer` (`buyer_id`) COVERING (`order_id`)
PARTITION BY KEY(`buyer_id`)
PARTITIONS 16,
GLOBAL INDEX `g_i_seller` (`seller_id`) COVERING (`order_id`)
PARTITION BY KEY(`seller_id`)
PARTITIONS 16,
KEY `l_i_order` (`order_id`),
KEY `l_i_buyer` (`buyer_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8
PARTITION BY KEY(`order_id`)
PARTITIONS 16
一層索引指定
通過FORCE INDEX
指定局部索引l_i_order
,示例如下:
mysql> EXPLAIN SELECT * FROM T_ORDER FORCE INDEX(l_i_order);
+---------------------+
| LOGICAL EXECUTIONPLAN
+---------------------+
| Gather(concurrent=true)
| LogicalView(tables="t_order[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `buyer_id`, `seller_id` FROM `T_ORDER` AS `T_ORDER` FORCE INDEX(L_I_ORDER)")
| HitCache:false
| Source:PLAN_CACHE
| TemplateId: 3d1b35fa
+---------------------+
通過INDEX HINT
指定全局索引g_i_buyer
,示例如下:
mysql> EXPLAIN /*TDDL:INDEX(T_ORDER, g_i_buyer)*/ SELECT * FROM T_ORDER;
+---------------------+
| LOGICAL EXECUTIONPLAN
+---------------------+
| Project(id="id", order_id="order_id", buyer_id="buyer_id", seller_id="seller_id")
| BKAJoin(condition="id = id AND order_id = order_id", type="inner")
| Gather(concurrent=true)
| IndexScan(tables="g_i_buyer_$27c2[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `buyer_id` FROM `g_i_buyer_$27c2` AS `g_i_buyer_$27c2`")
| Gather(concurrent=true)
| LogicalView(tables="t_order[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `seller_id` FROM `T_ORDER` AS `T_ORDER` FORCE INDEX(PRIMARY) WHERE (((`id`, `order_id`)) IN (...))")
| HitCache:false
| Source:null
| TemplateId: NULL
+---------------------+
二層索引指定
通過FORCE INDEX
指定全局索引g_i_buyer
的局部索引auto_shard_key_buyer_id
,示例如下:
mysql> EXPLAIN SELECT * FROM T_ORDER FORCE INDEX(g_i_buyer.auto_shard_key_buyer_id);
+---------------------+
| LOGICAL EXECUTIONPLAN
+---------------------+
| Project(id="id", order_id="order_id", buyer_id="buyer_id", seller_id="seller_id")
| BKAJoin(condition="id = id AND order_id = order_id", type="inner")
| Gather(concurrent=true)
| IndexScan(tables="g_i_buyer_$27c2[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `buyer_id` FROM `g_i_buyer_$27c2` AS `g_i_buyer_$27c2` FORCE INDEX(AUTO_SHARD_KEY_BUYER_ID)")
| Gather(concurrent=true)
| LogicalView(tables="t_order[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `seller_id` FROM `T_ORDER` AS `T_ORDER` FORCE INDEX(PRIMARY) WHERE (((`id`, `order_id`)) IN (...))")
| HitCache:false
| Source:PLAN_CACHE
| TemplateId: ae278c70
+---------------------+
通過INDEX HINT
指定別名表t1的全局索引g_i_seller
的局部索引auto_shard_key_seller_id
,以及別名表t2的主表的局部索引L_I_ORDER
,示例如下:
mysql> EXPLAIN /*TDDL:index(t1, g_i_seller, auto_shard_key_seller_id) index(t2, primary, L_I_ORDER)*/ SELECT * FROM t_order t1 join t_order t2 on t1.id=t2.id where t1.buyer_id = '123456';
+---------------------+
| LOGICAL EXECUTIONPLAN
+---------------------+
| BKAJoin(condition="id = id", type="inner")
| Project(id="id", order_id="order_id", buyer_id="buyer_id", seller_id="seller_id")
| BKAJoin(condition="id = id AND order_id = order_id", type="inner")
| Gather(concurrent=true)
| IndexScan(tables="g_i_seller_$3e3d[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `seller_id` FROM `g_i_seller_$3e3d` AS `g_i_seller_$3e3d` FORCE INDEX(AUTO_SHARD_KEY_SELLER_ID)")
| Gather(concurrent=true)
| LogicalView(tables="t_order[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `buyer_id` FROM `t_order` AS `t_order` FORCE INDEX(PRIMARY) WHERE ((`buyer_id` = ?) AND (((`id`, `order_id`)) IN (...)))")
| Gather(concurrent=true)
| LogicalView(tables="t_order[p1,p2,p3,...p16]", shardCount=16, sql="SELECT `id`, `order_id`, `buyer_id`, `seller_id` FROM `t_order` AS `t_order` FORCE INDEX(L_I_ORDER) WHERE (`id` IN (...))")
| HitCache:false
| Source:null
| TemplateId: NULL
+---------------------+