为什么 Postgres 的规划时间和执行时间如此不同? [英] Why Planing time and Execution time are so different Postgres?

查看:87
本文介绍了为什么 Postgres 的规划时间和执行时间如此不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了这样的查询

EXPLAIN (ANALYZE ,BUFFERS )
        SELECT COUNT(id) q, day
        FROM my_table
        WHERE role_id && ARRAY[15, 17]
        GROUP BY "day"
        ORDER BY "day" DESC;

Postgres 回复我:

And Postgres responds me with this:

规划时间:0.286 ms
执行时间:127.233 ms

Planning time: 0.286 ms
Execution time: 127.233 ms

这是为什么?我觉得差距太大了

Why is this ? The difference is too big I think

推荐答案

我认为您的理解有一些小误会.我试着简短地描述一下当你运行一个查询时发生了什么:

I think there's small misunderstanding of yours. I try to describe shortly what's going on when you run a query:

  1. 您用 SQL 编写一个查询,它是某种脚本",您试图告诉服务器您想从他那里得到什么.
  2. 大多数情况下,服务器可以通过多种方式通过编写查询来收集您要求的数据.有一种叫做查询计划器"的机制发挥作用.它试图找到执行查询的最快方式(计划).它是通过估计几种可能方式(计划)的执行时间来实现的.
  3. 服务器使用被认为最快的计划运行查询.
  4. 服务器将输出返回给您.

EXPLAIN 命令打印出该过程的描述.现在:

EXPLAIN command prints you description of that process. Now:

  • 执行时间 EXPLAIN 输出是服务器在步骤 3-4 上花费的时间.
  • 规划时间 EXPLAIN 输出是时间服务器花费在步骤 2 上.我相信您将其视为时间规划器认为查询需要花费的时间",但这可以称为计划的 [执行] 时间"或估计的执行时间".
  • Execution time on EXPLAIN output is time server spent on steps 3-4.
  • Planning time on EXPLAIN output is time server spent on step 2 only. I believe you think of it as "time planner thinks that query would take", but that can be called "planned [execution] time" or "estimated execution time".

所以没有理由为什么计划时间和执行时间差应该更小.PostgreSQL 希望缩短计划时间,以尽量减少它对整个执行时间的影响.

所有内容都写在这里的手册.

注意:执行时间不包括PlanningTime,你可以试试explain analysis select 1PlanningTime>的情况.执行时间.

Notice: Execution time not includes Planning time, you can try explain analyse select 1 to see a case where PlanningTime>ExecutionTime.

这篇关于为什么 Postgres 的规划时间和执行时间如此不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆