DBAtlas · 数据库运维图谱

验证分区裁剪

Oracle

EXPLAIN PLAN FOR
SELECT * FROM sale WHERE sale_date = DATE '2026-01-15';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
-- Look for 'PARTITION RANGE ITERATOR/SINGLE' and the Pstart/Pstop columns in the plan.

Pstart/Pstop 显示只读取了相关分区(理想情况是数字而不是 KEY)。对分区键应用函数/隐式转换会阻止裁剪。

PostgreSQL

SET enable_partition_pruning = on;
EXPLAIN (ANALYZE)
SELECT * FROM sale WHERE sale_date = DATE '2026-01-15';
-- Verify in the plan that only the relevant partition (child table) is scanned.

开启 enable_partition_pruning 后,计划中只出现匹配的分区;其他分区显示为 'never executed'。常量值在计划时裁剪,参数在执行时裁剪。

MySQL

-- Show which partitions are scanned:
EXPLAIN SELECT * FROM sale WHERE sale_date = '2026-01-15';
-- 5.7 and earlier: EXPLAIN PARTITIONS SELECT ... (removed in 8.0)

在 MySQL 5.7+ 中,普通 EXPLAIN 输出的 'partitions' 列列出将要扫描的分区(EXPLAIN PARTITIONS 语法已在 8.0 中移除);应只出现相关分区。如果列出了所有分区,说明没有进行裁剪。

SQL Server

-- Find which partition a value falls into:
SELECT $PARTITION.pf_sale_date('2026-01-15') AS partition_no;
-- Examine 'Actual Partition Count' and 'Actual Partitions Accessed' in the actual plan.

$PARTITION 函数返回某个值所在的分区号。如果实际执行计划中访问的分区数接近 1,说明裁剪已生效;如果扫描了所有分区,说明谓词不匹配。

MongoDB

// Targeted query using the shard key in a sharded cluster:
db.sale.find({ shop_id: 42 }).explain("executionStats")
// In explain, only the relevant shard should appear under 'shards'.

如果查询包含分片键,mongos 只将其路由到相关分片(targeted);否则会分发到所有分片(scatter-gather)。在 explain 输出中确认访问了哪些分片。

Redis

# Not applicable: there is no table/partition concept.

数据通过 Cluster 哈希槽分布;不存在查询裁剪,而是直接按键访问。

Elasticsearch

# Route to the relevant shard with routing:
GET <index>/_search?routing=customer42
{ "query": { "term": { "customer_id": 42 } } }
# Query only the relevant indices with a time-based index pattern:
GET sale-2026-01/_search

自定义 routing 将查询收窄到单个分片(类似裁剪)。对于时间序列数据,带日期模式的索引名(sale-2026-01)可确保只搜索相关索引;应使用窄模式而非通配符。

在 DBAtlas 中打开 →