为什么Postgres的EXPLAIN ANALYZE执行时间与实际查询运行时间不同?

6

我正在使用数据库客户端进行测试。

使用EXPLAIN ANALYZE

Hash Join  (cost=5.02..287015.54 rows=3400485 width=33) (actual time=0.023..1725.842 rows=3327845 loops=1)
  Hash Cond: ((fact_orders.financial_status)::text = (include_list.financial_status)::text)
  CTE include_list
    ->  Result  (cost=0.00..1.77 rows=100 width=32) (actual time=0.003..0.004 rows=4 loops=1)
          ->  ProjectSet  (cost=0.00..0.52 rows=100 width=32) (actual time=0.002..0.003 rows=4 loops=1)
                ->  Result  (cost=0.00..0.01 rows=1 width=0) (actual time=0.000..0.000 rows=1 loops=1)
  ->  Seq Scan on fact_orders  (cost=0.00..240253.85 rows=3400485 width=38) (actual time=0.006..551.558 rows=3400485 loops=1)
  ->  Hash  (cost=2.00..2.00 rows=100 width=32) (actual time=0.009..0.009 rows=4 loops=1)
        Buckets: 1024  Batches: 1  Memory Usage: 9kB
        ->  CTE Scan on include_list  (cost=0.00..2.00 rows=100 width=32) (actual time=0.004..0.007 rows=4 loops=1)
Planning time: 0.163 ms
Execution time: 1852.226 ms

根据上述查询,我的执行时间为1852.226毫秒。
大约返回了330万条记录。
但是如果我在不使用"EXPLAIN ANALYZE"的情况下运行查询,则需要大约30秒才能从数据库客户端获取结果。
额外的28秒是从服务器传输到客户端的时间吗?还是实际执行查询的时间?
编辑:客户端是Navicat。 使用屏幕上呈现结果后经过的时间。

你使用哪个客户端,如何测量时间? - Laurenz Albe
我正在使用Navicat。时间是客户端将结果呈现到屏幕上后显示的“经过的时间”。@LaurenzAlbe - john
EXPLAIN ANALYZE 花了 30 秒才出现在屏幕上,还是 SELECT...?我问这个问题是因为可能需要将大量数据导入 Navicat。 - richyen
没有EXPLAIN ANALYZE是瞬间的。 - john
1个回答

6

根据文档说明

请注意,只有在使用ANALYZE选项时才会实际执行语句。虽然EXPLAIN会丢弃SELECT返回的任何输出,但语句的其他副作用将像平常一样发生。

因此,仅运行select查询的解释和实际查询之间的唯一区别是实际上不会获取数据。您的查询返回大量记录,这可能很好地解释了您所看到的差异。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接