【问题标题】:Why is Postgres EXPLAIN ANALYZE execution_time different than when I run the actual query?为什么 Postgres EXPLAIN ANALYZE execution_time 与我运行实际查询时不同?
【发布时间】:2020-03-08 16:49:58
【问题描述】:

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

使用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。使用结果显示到屏幕后经过的时间。

【问题讨论】:

  • 您使用的是哪个客户端,您如何测量时间?
  • 我正在使用 Navicat。时间是客户端将结果显示到屏幕后显示的“经过的时间”。 @LaurenzAlbe
  • EXPLAIN ANALYZE 是否需要 30 才能到达屏幕,还是 SELECT...?我问 bc 也许是需要摄取到 Navicat 的数据量
  • 不,EXPLAIN ANALYZE 是即时的。

标签: postgresql sql-execution-plan explain


【解决方案1】:

documentation says:

请记住,当使用ANALYZE 选项时,该语句实际上会被执行。尽管EXPLAIN 将丢弃SELECT 将返回的任何输出,但该语句的其他副作用将照常发生。

因此,在 select 查询上运行解释与运行实际查询之间的唯一区别是,实际上并未获取数据。您的查询返回大量记录,因此只能很好地解释您所看到的差异。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2017-08-03
    • 1970-01-01
    相关资源
    最近更新 更多