理解PostgreSQL查询执行计划(三)--复杂操作篇
在本系列的前一篇文章中,我们探讨了如何解读执行计划输出的单行结构,并介绍了所有基本的数据获取操作(解释树中的节点)。今天,我们将深入讨论更复杂的操作。
函数扫描(Function Scan)
示例:
EXPLAIN ANALYZE SELECT * FROM generate_series(1,10) i;QUERY PLAN
---------------------------------------------------------------------------------------------------------------------Function Scan on generate_series i (cost=0.00..10.00 rows=1000 width=4) (actual time=0.057..0.057 rows=10 loops=1)
函数扫描是一个非常简单的节点 - 它执行一个返回记录集的函数(与lower()
这类标量函数不同)。函数返回的行会被传递到执行计划树的上层节点,或者如果函数扫描是顶级节点,则直接返回给客户端。
函数扫描还可以对返回的行进行过滤:
EXPLAIN ANALYZE SELECT * FROM generate_series(1,10) i WHERE i < 3;QUERY PLAN
-------------------------------------------------------------------------------------------------------------------Function Scan on generate_series i (cost=0.00..12.50 rows=333 width=4) (actual time=0.008..0.009 rows=2 loops=1)Filter: (i < 3)Rows Removed by Filter: 8
排序操作(Sort)
排序操作获取输入记录集并按指定方式排序后输出。
示例:
EXPLAIN ANALYZE SELECT * FROM pg_class ORDER BY relname;QUERY PLAN
---------------------------------------------------------------------------------------------------------------Sort (cost=29.41..30.29 rows=352 width=226) (actual time=2.102..2.393 rows=357 loops=1)Sort Key: relnameSort Method: quicksort Memory: 119kB-> Seq Scan on pg_class (cost=0.00..14.52 rows=352 width=226) (actual time=0.126..0.415 rows=357 loops=1)
排序操作有几个重要特性:
- 当排序内存超过
work_mem
设置时,会切换到基于磁盘的排序:
EXPLAIN ANALYZE SELECT random() AS x FROM generate_series(1,14000) i ORDER BY x;Sort Method: quicksort Memory: 1041kB
EXPLAIN ANALYZE SELECT random() AS x FROM generate_series(1,160000) i ORDER BY x;Sort Method: external merge Disk: 2464kB
- 当排序与LIMIT结合使用时,会采用更高效的"top-N heapsort"算法:
EXPLAIN ANALYZE SELECT * FROM pg_class ORDER BY relfilenode LIMIT 5;Sort Method: top-N heapsort Memory: 26kB
这种优化将排序复杂度从O(mlog(m))降低到O(mlog(n)),其中m是总行数,n是LIMIT指定的行数。
限制操作(Limit)
Limit操作执行其子操作,但只返回前N行。通常它会提前终止子操作的执行。
示例对比:
EXPLAIN ANALYZE SELECT * FROM pg_class;QUERY PLAN
---------------------------------------------------------------------------------------------------------Seq Scan on pg_class (cost=0.00..14.52 rows=352 width=226) (actual time=0.008..0.043 rows=357 loops=1)
EXPLAIN ANALYZE SELECT * FROM pg_class LIMIT 2;QUERY PLAN
-------------------------------------------------------------------------------------------------------------Limit (cost=0.00..0.08 rows=2 width=226) (actual time=0.009..0.010 rows=2 loops=1)-> Seq Scan on pg_class (cost=0.00..14.52 rows=352 width=226) (actual time=0.008..0.008 rows=2 loops=1)
哈希聚合(HashAggregate)
当查询包含GROUP BY或聚合函数(如sum()、avg()等)时,会使用此操作。
示例:
EXPLAIN ANALYZE SELECT relkind, COUNT(*) FROM pg_class GROUP BY relkind;QUERY PLAN
-------------------------------------------------------------------------------------------------------------HashAggregate (cost=16.28..16.34 rows=6 width=9) (actual time=0.151..0.153 rows=6 loops=1)Group Key: relkind-> Seq Scan on pg_class (cost=0.00..14.52 rows=352 width=1) (actual time=0.009..0.042 rows=357 loops=1)
工作原理:
- 为每行计算GROUP BY键值
- 在哈希表中维护每个键的聚合状态
- 处理完所有行后输出结果
同样受work_mem
限制,当哈希表过大时会使用磁盘存储。
哈希连接(Hash Join)
哈希连接用于连接两个数据集,包含两个子操作:一个总是"Hash"操作,另一个是任意操作。
示例:
EXPLAIN ANALYZE SELECT * FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid;QUERY PLAN
------------------------------------------------------------------------------------------------------------------------Hash Join (cost=1.25..20.61 rows=352 width=339) (actual time=0.028..0.217 rows=357 loops=1)Hash Cond: (c.relnamespace = n.oid)-> Seq Scan on pg_class c (cost=0.00..14.52 rows=352 width=226) (actual time=0.006..0.037 rows=357 loops=1)-> Hash (cost=1.11..1.11 rows=11 width=117) (actual time=0.015..0.015 rows=11 loops=1)Buckets: 1024 Batches: 1 Memory Usage: 10kB-> Seq Scan on pg_namespace n (cost=0.00..1.11 rows=11 width=117) (actual time=0.009..0.011 rows=11 loops=1)
工作流程:
- 先执行Hash子操作构建哈希表
- 然后执行另一侧操作,对每行在哈希表中查找匹配
- 找到匹配则输出连接结果
嵌套循环连接(Nested Loop)
嵌套循环连接有两个子操作,对左侧结果的每一行执行右侧操作。
示例:
EXPLAIN ANALYZE SELECT a.* FROM pg_class c JOIN pg_attribute a ON c.oid = a.attrelid
WHERE c.relname IN ('pg_class', 'pg_namespace');QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------Nested Loop (cost=8.84..55.73 rows=15 width=203) (actual time=0.018..0.043 rows=48 loops=1)-> Bitmap Heap Scan on pg_class c (cost=8.56..14.03 rows=2 width=4) (actual time=0.011..0.012 rows=2 loops=1)Recheck Cond: (relname = ANY ('{pg_class,pg_namespace}'::name[]))Heap Blocks: exact=2-> Bitmap Index Scan on pg_class_relname_nsp_index (cost=0.00..8.56 rows=2 width=0) (actual time=0.009..0.009 rows=2 loops=1)Index Cond: (relname = ANY ('{pg_class,pg_namespace}'::name[]))-> Index Scan using pg_attribute_relid_attnum_index on pg_attribute a (cost=0.28..20.77 rows=8 width=203) (actual time=0.004..0.007 rows=24 loops=2)Index Cond: (attrelid = c.oid)
注意Index Scan的loops=2
表示该操作执行了两次。
合并连接(Merge Join)
当连接的数据集已按连接键排序时使用此方法。
示例(强制使用排序):
EXPLAIN ANALYZE SELECT * FROM(SELECT oid, * FROM pg_class ORDER BY oid) AS cJOIN(SELECT * FROM pg_attribute a ORDER BY attrelid) AS aON c.oid = a.attrelid;QUERY PLAN---------------------------------------------------------------------------------------------------------------------------------------------------------------
-------Merge Join (cost=29.69..352.26 rows=2716 width=433) (actual time=0.347..10.218 rows=2749 loops=1)Merge Cond: (pg_class.oid = a.attrelid)-> Sort (cost=29.41..30.29 rows=352 width=230) (actual time=0.299..0.363 rows=357 loops=1)Sort Key: pg_class.oidSort Method: quicksort Memory: 119kB-> Seq Scan on pg_class (cost=0.00..14.52 rows=352 width=230) (actual time=0.014..0.147 rows=357 loops=1)-> Materialize (cost=0.28..283.62 rows=2716 width=203) (actual time=0.017..8.655 rows=2749 loops=1)-> Index Scan using pg_attribute_relid_attnum_index on pg_attribute a (cost=0.28..249.67 rows=2716 width=203) (actual time=0.016..8.222 rows=2749 lo
ops=1)
工作流程:
- 同时扫描两个已排序的输入集
- 比较当前行的连接键
- 根据比较结果决定从哪一侧获取下一行
物化操作(Materialize)
物化操作将底层操作的结果存储在内存中,供多次使用。
示例:
EXPLAIN ANALYZE \dTSMaterialize (cost=0.00..1.17 rows=11 width=68) (actual time=0.000..0.001 rows=11 loops=95)-> Seq Scan on pg_namespace n (cost=0.00..1.11 rows=11 width=68) (actual time=0.004..0.006 rows=11 loops=1)
在这个例子中,物化避免了95次表扫描,只需扫描一次并将结果存储在内存中。
连接操作的变体
连接操作有以下变体:
- 左/右连接:Hash Left Join, Merge Left Join等
- 全连接:Hash Full Join, Merge Full Join
- 反连接:Hash Anti Join(用于NOT EXISTS子查询)
示例(反连接):
EXPLAIN ANALYZE SELECT * FROM pg_class c
WHERE NOT EXISTS (SELECT * FROM pg_attribute a WHERE a.attrelid = c.oid AND a.attnum = 10);Hash Anti Join (cost=93.62..115.69 rows=298 width=226) (actual time=0.642..0.851 rows=303 loops=1)
反连接只返回在另一侧找不到匹配的行。
通过本文,我们详细探讨了PostgreSQL中的各种复杂操作。在后续文章中,我们将继续介绍其他操作类型和执行计划的统计信息。
原文链接:https://mp.weixin.qq.com/s/yY86kVfTjC056xKB8_FCUw