我认为在关于解释计划的postgres文档中发现了一个错误和可能的纠正。
来自:https://www.postgresql.org/docs/current/using-explain.html
Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.29..7.91 rows=1 width=244) (actual time=0.021..0.022 rows=1 loops=10)
“在上面的例子中,我们总共花了0.220毫秒来执行tenk2上的索引扫描。”
文档似乎表明Actual Total Time
* Actual Loops
=在操作上花费的总时间。
但是,根据我制作的JSON计划:
"Plans": [
{
"Node Type": "Hash Join",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Join Type": "Inner",
"Startup Cost": 66575.34,
"Total Cost": 76861.82,
"Plan Rows": 407,
"Plan Width": 290,
"Actual Startup Time": 49962.789,
"Actual Total Time": 51206.643,
"Actual Rows": 127117,
"Actual Loops": 3,
"Output": [ ... ],
...
"Execution Time": 52677.398
(完整的计划是here。)
Actual Total Time
* Actual Loops
= 51秒* 3 = 2分33秒明显超过52.7秒的Execution Time
。
我是否正确理解文档?
如果是这样,不应该说,“我们总共花了0.01毫秒来执行tenk2上的索引扫描”?
你的Hash Join
位于Gather
节点下面:
Gather (cost=67,575.34..77,959.52 rows=977 width=290) (actual time=51,264.085..52,595.474 rows=381,352 loops=1)
Buffers: shared hit=611279 read=99386
-> Hash Join (cost=66,575.34..76,861.82 rows=407 width=290) (actual time=49,962.789..51,206.643 rows=127,117 loops=3)
Buffers: shared hit=611279 read=99386
这意味着查询启动了两个后台工作程序,它们与主后端并行运行以完成散列连接(请参阅执行计划中的"Workers Launched": 2
)。
现在显而易见的是,如果三个进程对任务起作用,则总执行时间将不是各个执行时间的总和。
换句话说,执行时间与循环次数相乘的规则适用于嵌套循环连接(单线程),但不适用于查询的并行执行。