获取现有的 Postgresql SQL 计划



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

我知道甲骨文有一个观点叫做DBA_HIST_SQL_PLAN试图谷歌"现有或旧的SQL计划PostgreSQL",但找不到相关的东西。

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

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

不清楚你在这里说的"旧"是什么意思:

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

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

如果您愿意减慢查询速度并生成更多的日志记录输出,则可以将 PostgreSQL 配置为针对满足某些指定条件的查询自动将执行计划写入日志:

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

最新更新