【问题标题】:get existing SQL plan for Postgresql获取 Postgresql 的现有 SQL 计划
【发布时间】:2018-02-01 20:41:47
【问题描述】:

我想在我的 PostgreSQL 中查看我现有的旧 SQL 计划

我知道 Oracle 有一个名为 DBA_HIST_SQL_PLAN 的视图 试图用谷歌搜索“现有的或旧的 SQL 计划 PostgreSQL,但找不到相关的东西。

我不想使用PREPARE STATMENT - 因为它会限制我。

如何在我的 PostgreSQL 数据库中获取现有计划

【问题讨论】:

标签: sql postgresql sql-execution-plan


【解决方案1】:

这里的“旧”是什么意思并不清楚:

我想在我的 PostgreSQL 中查看我现有的旧 SQL 计划

我假设您需要在注意到要检查的查询后找到记录的 EXPLAIN 输出(执行计划)。

如果您愿意放慢查询速度并生成更多日志输出,您可以自动 configure PostgreSQL to write the execution plan to the log 处理满足某些指定条件的查询:

auto_explain 模块提供了一种自动记录慢速语句执行计划的方法,而无需手动运行EXPLAIN。这对于跟踪大型应用程序中未优化的查询特别有用。 [...] 当然,开销是有代价的。

[…] 有几个配置参数控制auto_explain 的行为。请注意,默认行为是什么都不做,因此如果您想要任何结果,您必须至少设置 auto_explain.log_min_duration

[…]

示例

postgres=# LOAD 'auto_explain';
postgres=# SET auto_explain.log_min_duration = 0;
postgres=# SET auto_explain.log_analyze = true;
postgres=# SELECT count(*)
           FROM pg_class, pg_index
           WHERE oid = indrelid AND indisunique;

这可能会产生日志输出,例如:

LOG:  duration: 3.651 ms  plan:
  Query Text: SELECT count(*)
              FROM pg_class, pg_index
              WHERE oid = indrelid AND indisunique;
  Aggregate  (cost=16.79..16.80 rows=1 width=0) (actual time=3.626..3.627 rows=1 loops=1)
    ->  Hash Join  (cost=4.17..16.55 rows=92 width=0) (actual time=3.349..3.594 rows=92 loops=1)
          Hash Cond: (pg_class.oid = pg_index.indrelid)
          ->  Seq Scan on pg_class  (cost=0.00..9.55 rows=255 width=4) (actual time=0.016..0.140 rows=255 loops=1)
          ->  Hash  (cost=3.02..3.02 rows=92 width=4) (actual time=3.238..3.238 rows=92 loops=1)
                Buckets: 1024  Batches: 1  Memory Usage: 4kB
                ->  Seq Scan on pg_index  (cost=0.00..3.02 rows=92 width=4) (actual time=0.008..3.187 rows=92 loops=1)
                      Filter: indisunique

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-29
    • 2022-10-19
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多