我正在尝试在我的Postgres查询中确定一些性能瓶颈,并对查询运行{() => this.fectchImage()}
以获取一些见解。查询分析的输出如下:
EXPLAIN ANALYZE
我之前没有对pgexplain做很多查询分析,所以我仍在努力将它在这里告诉我的一切都包裹住。我看到的一件事使我有些困惑,因为我可以看到外部执行:
Nested Loop (cost=162.35..5361.39 rows=4385 width=33) (actual time=5.663..315.153 rows=2 loops=1)
-> Seq Scan on "User" p=u (cost=0.00..1.02 rows=1 width=8) (actual time=0.009..0.011 rows=1 loops=1)
Filter: ("ID" = 1)
Rows Removed by Filter: 1
-> Nested Loop (cost=162.35..5316.51 rows=4385 width=33) (actual time=5.646..315.130 rows=2 loops=1)
-> Nested Loop (cost=161.93..1854.59 rows=6574 width=33) (actual time=5.567..106.350 rows=44342 loops=1)
-> Seq Scan on "Op" o (cost=0.00..1.34 rows=1 width=8) (actual time=0.007..0.011 rows=1 loops=1)
Filter: ("Name" = 'write'::text)
Rows Removed by Filter: 26
-> Bitmap Heap Scan on "Account" a (cost=161.93..1768.73 rows=8453 width=33) (actual time=5.551..45.435 rows=44342 loops=1)
Recheck Cond: ("OId" = o."ID")
Filter: ("UId" = 1)
Heap Blocks: exact=1480
-> Bitmap Index Scan on "IX_Account_op_ID" (cost=0.00..159.82 rows=8453 width=0) (actual time=5.138..5.139 rows=44342 loops=1)
Index Cond: ("OId" = o."ID")
-> Index Scan using "PK_Resource_ID" on "Resources" r (cost=0.42..0.53 rows=1 width=8) (actual time=0.003..0.003 rows=0 loops=44342)
Index Cond: ("ID" = a."ResourceId")
Filter: ("Role" = ANY ('{r1,r2,r3,r4,r5}'::text[]))
Rows Removed by Filter: 1
Planning Time: 0.777 ms
Execution Time: 315.220 ms
说实际时间是5.663 ... 315-好的,这是有道理的,因为总执行时间是315。然后,比该时间低一点:
嵌套循环(成本= 162.35..5316.51行= 4385宽度= 33)(实际时间= 5.646..315.130行= 2个循环= 1)
好!这告诉我总执行时间的大部分将在本节中。在此之下,我看到:
Nested Loop (cost=162.35..5361.39 rows=4385 width=33) (actual time=5.663..315.153 rows=2 loops=1)
嗯-就是说有一个嵌套循环,它使用 -> Nested Loop (cost=161.93..1854.59 rows=6574 width=33) (actual time=5.567..106.350 rows=44342 loops=1)
-> Seq Scan on "Op" o (cost=0.00..1.34 rows=1 width=8) (actual time=0.007..0.011 rows=1 loops=1)
Filter: ("Name" = 'write'::text)
Rows Removed by Filter: 26
-> Bitmap Heap Scan on "Account" a (cost=161.93..1768.73 rows=8453 width=33) (actual time=5.551..45.435 rows=44342 loops=1)
Recheck Cond: ("OId" = o."ID")
Filter: ("UId" = 1)
Heap Blocks: exact=1480
,该循环循环了一次,而该循环的内容是一个使用106.35 ms
的sq扫描和一个位图堆扫描,拿了.011ms
。
所有循环都具有1个循环,但在两种情况下,对我而言,这些循环的总执行量都高于循环内容的总和。在内部循环的情况下,它花费了45.435ms
来执行,但是如果我将那个循环的内容加起来,看起来它应该只花费了106ms
45.446ms
。在外循环的情况下,它花费了(.011ms + 45.435ms)
,但是如果我加总内容,它看起来应该是315.13ms
106.353ms
。
我正在研究这个问题,并假设loops = 1意味着它只执行一次该循环中的内容。尽管总的时间表明执行了不止一次。我不确定我在哪里曲解。有人可以帮我一下吗?非常感谢这里的任何建议!
答案 0 :(得分:1)
ANALYZE不显示在where createdate >= trunc(sysdate)
and createdate < trunc(sysdate) + 1
中执行任务所花费的时间。
这里的SELECT tasks FROM [..]
花费了20000毫秒(20x1秒),但是您只能在pg_sleep(1)
部分看到它
Nested Loop
它没有显示explain (analyze, verbose)
select *, pg_sleep(1)
from generate_series(1,5) AS w
cross join generate_series(current_date,current_date+3,interval '1 day') AS v;
QUERY PLAN
Nested Loop (cost=0.02..22510.02 rows=1000000 width=16) (actual time=1001.082..20021.134 rows=20 loops=1)
Output: w.w, v.v, pg_sleep('1'::double precision)
-> Function Scan on pg_catalog.generate_series w (cost=0.00..10.00 rows=1000 width=4) (actual time=0.006..0.008 rows=5 loops=1)
Output: w.w
Function Call: generate_series(1, 5)
-> Function Scan on pg_catalog.generate_series v (cost=0.02..10.02 rows=1000 width=8) (actual time=0.005..0.010 rows=4 loops=5)
Output: v.v
Function Call: generate_series((('now'::cstring)::date)::timestamp with time zone, ((('now'::cstring)::date + 3))::timestamp with time zone, '1 day'::interval)
Planning time: 0.093 ms
Execution time: 20021.178 ms
数据集需要花费多长时间。在这里获取数据大约需要1000毫秒,但是加入这些集合所需的时间要长4倍,因为您在JOIN
部分中看到的时间增加了大约那么多,这是唯一要做的事情,并且未在详细计划中列出。
Nested Loop