Query a specific partition
Partition pruning is automatic — the optimizer infers which partitions to scan from the WHERE clause. Partition selection is explicit — you name the partitions in the statement itself, and only those partitions are searched, even if matching rows exist elsewhere. Use partition selection when the WHERE clause does not contain enough information for the optimizer to prune automatically, or when you already know which partitions are relevant.
Partition selection is supported on SELECT, UPDATE, and DELETE statements in PolarDB-X automatic partitioning mode (AUTO mode).
SQL syntax
SELECT
SELECT ... FROM tbl_name [PARTITION ( part_name[, part_name, ...] )]UPDATE
UPDATE tbl_name [PARTITION ( part_name[, part_name, ...] )] SET ... WHERE ...DELETE
DELETE FROM tbl_name [PARTITION ( part_name[, part_name, ...] )] WHERE ...Query data from partitions
The following examples use a table partitioned by KEY(id, bid) with 8 partitions.
Example 1: Read from specific partitions
CREATE TABLE tb_k(
id bigint not null auto_increment,
bid int,
name varchar(30),
birthday datetime not null,
primary key(id)
)
PARTITION BY KEY(id, bid)
PARTITIONS 8;explain SELECT * FROM tb_k PARTITION( p1,p2 );+-----------------------------------------------------------------------------------------------------------------------+
| LOGICAL EXECUTIONPLAN |
+-----------------------------------------------------------------------------------------------------------------------+
| Gather(concurrent=true) |
| LogicalView(tables="tb_k[p1,p2]", shardCount=2, sql="SELECT `id`, `bid`, `name`, `birthday` FROM `tb_k` AS `tb_k`") |
| HitCache:false |
| Source:PLAN_CACHE |
| TemplateId: e210fe50 |
+-----------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.05 sec)The logical execution plan confirms that only p1 and p2 are accessed (shardCount=2).
Example 2: Delete from specific partitions
explain DELETE FROM tb_k PARTITION( p1,p2 );+---------------------------------------------------------------------------------+
| LOGICAL EXECUTIONPLAN |
+---------------------------------------------------------------------------------+
| LogicalModifyView(tables="tb_k[p1,p2]", shardCount=2, sql="DELETE FROM `tb_k`") |
| HitCache:false |
| Source:PLAN_CACHE |
| TemplateId: 19bd2adf |
+---------------------------------------------------------------------------------+The PARTITION clause restricts the search scope of the WHERE condition. Only rows in the named partitions (p1 and p2) are deleted. Rows in other partitions that satisfy the WHERE condition are not affected.
Query data from subpartitions
The following example uses a table with two-level partitioning: PARTITION BY KEY(bid,name) PARTITIONS 2 at the top level and SUBPARTITION BY KEY(id) SUBPARTITIONS 4 at the subpartition level, producing subpartitions named p1sp1–p1sp4 and p2sp1–p2sp4.
Example 3: Read from specific partitions or subpartitions
CREATE TABLE tb_k_k_tp(
id bigint not null auto_increment,
bid int,
name varchar(30),
birthday datetime not null,
primary key(id)
)
PARTITION BY KEY(bid,name)
PARTITIONS 2
SUBPARTITION BY KEY(id)
SUBPARTITIONS 4;Specify two subpartitions:
explain SELECT * FROM tb_k_k_tp PARTITION( p1sp1,p1sp2 )/* Specify two subpartitions. */;+--------------------------------------------------------------------------------------------------------------------------------------------+
| LOGICAL EXECUTIONPLAN |
+--------------------------------------------------------------------------------------------------------------------------------------------+
| Gather(concurrent=true) |
| LogicalView(tables="tb_k_k_tp[p1sp1,p1sp2]", shardCount=2, sql="SELECT `id`, `bid`, `name`, `birthday` FROM `tb_k_k_tp` AS `tb_k_k_tp`") |
| HitCache:false |
| Source:PLAN_CACHE |
| TemplateId: 38bba74d |
+--------------------------------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.03 sec)Specify a top-level partition and a subpartition together:
explain SELECT * FROM tb_k_k_tp PARTITION( p1,p2sp2 )/* Specify a partition and a subpartition. */;+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| LOGICAL EXECUTIONPLAN |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Gather(concurrent=true) |
| LogicalView(tables="tb_k_k_tp[p1sp1,p1sp2,p1sp3,p1sp4,p2sp2]", shardCount=5, sql="SELECT `id`, `bid`, `name`, `birthday` FROM `tb_k_k_tp` AS `tb_k_k_tp`") |
| HitCache:false |
| Source:PLAN_CACHE |
| TemplateId: dbc4cb56 |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.01 sec)Specifying a top-level partition (p1) includes all its subpartitions (p1sp1, p1sp2, p1sp3, p1sp4). An explicitly named subpartition from another partition (p2sp2) is added separately, giving a total shard count of 5.