Postgresql 输出 EXPLAIN ANALYZE 到文件
Postgresql output EXPLAIN ANALYZE to file
我需要知道特定查询 运行 需要多长时间(我希望 运行 时间很长)。为此,我决定在查询集上 运行 一个 EXPLAIN ANALYZE
,仅使用整个数据集的一部分并从那里进行推断。但是我有一个问题;查询需要两个多小时才能连接超时,没有结果。我不想增加超时,因为我不知道 运行 可能需要多长时间(在两小时到两天之间)。
有什么方法可以指示 SQL 服务器将数据输出到服务器文件系统上的文件中,这样我就不必担心超时?我试过以下方法:
Copy (
EXPLAIN ANALYZE INSERT INTO <table>
<Long complex query here>
) To '/tmp/analyze.csv' With CSV;
但我在 EXPLAIN
处收到错误消息。
郑重声明,是的,我想做 ANALYZE
因为
- 它减少了以后要处理的数据量,
- 它给出了实际时间估计。
非常简单的技巧:
create or replace function get_explain(in qry text, out r text) returns setof text as $$
begin
for r in execute qry loop
raise info '%', r;
return next;
end loop;
return;
end; $$ language plpgsql;
请注意,如果您不想真正修改数据,那么您应该将其包装到事务中:
begin;
copy (select get_explain('explain (analyze) select 1;')) to '/tmp/foo.foo';
select get_explain('explain (analyze, format xml) select 1;');
rollback;
可能已经存在可以使用的类似功能,但我没有找到。
PS:它会解决语法错误的问题,但我不确定它是否解决了超时问题,因为如文档中所述:
Important: Keep in mind that the statement is actually executed when
the ANALYZE option is used. Link.
您可以简单地使用 \o
in psql
将结果输出到文件:
# \o /tmp/output.txt
# explain analyze ...
# \o
\o
也可以通过管道传输到命令:查看 this blog post and of course the psql
documentation.
我需要知道特定查询 运行 需要多长时间(我希望 运行 时间很长)。为此,我决定在查询集上 运行 一个 EXPLAIN ANALYZE
,仅使用整个数据集的一部分并从那里进行推断。但是我有一个问题;查询需要两个多小时才能连接超时,没有结果。我不想增加超时,因为我不知道 运行 可能需要多长时间(在两小时到两天之间)。
有什么方法可以指示 SQL 服务器将数据输出到服务器文件系统上的文件中,这样我就不必担心超时?我试过以下方法:
Copy (
EXPLAIN ANALYZE INSERT INTO <table>
<Long complex query here>
) To '/tmp/analyze.csv' With CSV;
但我在 EXPLAIN
处收到错误消息。
郑重声明,是的,我想做 ANALYZE
因为
- 它减少了以后要处理的数据量,
- 它给出了实际时间估计。
非常简单的技巧:
create or replace function get_explain(in qry text, out r text) returns setof text as $$
begin
for r in execute qry loop
raise info '%', r;
return next;
end loop;
return;
end; $$ language plpgsql;
请注意,如果您不想真正修改数据,那么您应该将其包装到事务中:
begin;
copy (select get_explain('explain (analyze) select 1;')) to '/tmp/foo.foo';
select get_explain('explain (analyze, format xml) select 1;');
rollback;
可能已经存在可以使用的类似功能,但我没有找到。
PS:它会解决语法错误的问题,但我不确定它是否解决了超时问题,因为如文档中所述:
Important: Keep in mind that the statement is actually executed when the ANALYZE option is used. Link.
您可以简单地使用 \o
in psql
将结果输出到文件:
# \o /tmp/output.txt
# explain analyze ...
# \o
\o
也可以通过管道传输到命令:查看 this blog post and of course the psql
documentation.