获取postrgresql解释分析为列
因此,如果我对postgresql执行解释分析查询,我会得到以下结果: 查询计划 查询计划是列名,其他的是行,有没有办法在每一行都是列的情况下获取这些数据 比如: 过滤器|删除行|计划时间|执行时间| 材料| 38177324 |.0306 ms | 319613.03 ms|获取postrgresql解释分析为列,sql,postgresql,Sql,Postgresql,因此,如果我对postgresql执行解释分析查询,我会得到以下结果: 查询计划 查询计划是列名,其他的是行,有没有办法在每一行都是列的情况下获取这些数据 比如: 过滤器|删除行|计划时间|执行时间| 材料| 38177324 |.0306 ms | 319613.03 ms| create or replace function get_explain_as_json(text, out json) language plpgsql as $$ begin execute 'explain
create or replace function get_explain_as_json(text, out json) language plpgsql as $$
begin
execute 'explain (analyse, verbose, format json) ' || $1 into $2;
return;
end $$;
select
j,
j->0->>'Planning Time' as plan_time,
j->0->>'Execution Time' as exec_time,
j->0->'Plan'->>'Actual Rows' as total_rows
-- And so on...
from get_explain_as_json('select 1 where 2 = 3') as j;
或者将其包装到函数中:
create or replace function get_explain_as_json(text[]) returns setof json language plpgsql as $$
declare
q text;
j json;
begin
for q in select unnest($1) loop
execute 'explain (analyse, verbose, format json) ' || q into j;
return next j;
end loop;
return;
end $$;
with queries(q) as (values(array[
$$
select 1 where 2 = 3
$$,
$$
select 2 where 4 = 4
$$
]::text[]))
select ... from queries, get_explain_as_json(q) as j;
或使用参数(几乎与前面相同):
谢谢,这正是我所需要的,一个问题:有可能得到多个查询的解释吗?比如,这个解释有多行?@FernandoCastillaOspina你是说像
get\u explain\u as\u json(query1,query2,…)
这样的东西,它会为每个查询分别返回几行吗?在回顾中,这似乎很明显,不知道我怎么会错过它。谢谢@Abelisto:)你确定它能用吗?我添加了另一个查询,如get_explain_as_json('select1where2=3;select2where2=4')作为j代码>但是数据库显示没有results@FernandoCastillaOspina对不起,根据你的回答,我认为你自己找到了解决办法。答案已更新(但未测试)。
┌─────────────────────────────────────┬───────────┬───────────┬────────────┐
│ j │ plan_time │ exec_time │ total_rows │
╞═════════════════════════════════════╪═══════════╪═══════════╪════════════╡
│ [ ↵│ 0.042 │ 0.026 │ 0 │
│ { ↵│ │ │ │
│ "Plan": { ↵│ │ │ │
│ "Node Type": "Result", ↵│ │ │ │
│ "Parallel Aware": false, ↵│ │ │ │
│ "Startup Cost": 0.00, ↵│ │ │ │
│ "Total Cost": 0.01, ↵│ │ │ │
│ "Plan Rows": 1, ↵│ │ │ │
│ "Plan Width": 4, ↵│ │ │ │
│ "Actual Startup Time": 0.002,↵│ │ │ │
│ "Actual Total Time": 0.002, ↵│ │ │ │
│ "Actual Rows": 0, ↵│ │ │ │
│ "Actual Loops": 1, ↵│ │ │ │
│ "Output": ["1"], ↵│ │ │ │
│ "One-Time Filter": "false" ↵│ │ │ │
│ }, ↵│ │ │ │
│ "Planning Time": 0.042, ↵│ │ │ │
│ "Triggers": [ ↵│ │ │ │
│ ], ↵│ │ │ │
│ "Execution Time": 0.026 ↵│ │ │ │
│ } ↵│ │ │ │
│ ] │ │ │ │
└─────────────────────────────────────┴───────────┴───────────┴────────────┘
with queries(q) as (values(array[
$$
select 1 where 2 = 3
$$,
$$
select 2 where 4 = 4
$$
]::text[]))
select ... from queries, unnest(q) as q, get_explain_as_json(q) as j;
create or replace function get_explain_as_json(text[]) returns setof json language plpgsql as $$
declare
q text;
j json;
begin
for q in select unnest($1) loop
execute 'explain (analyse, verbose, format json) ' || q into j;
return next j;
end loop;
return;
end $$;
with queries(q) as (values(array[
$$
select 1 where 2 = 3
$$,
$$
select 2 where 4 = 4
$$
]::text[]))
select ... from queries, get_explain_as_json(q) as j;
create or replace function get_explain_as_json(variadic text[]) returns setof json language plpgsql as $$
declare
q text;
j json;
begin
for q in select unnest($1) loop
execute 'explain (analyse, verbose, format json) ' || q into j;
return next j;
end loop;
return;
end $$;
select ...
from get_explain_as_json($$select 1 where 2 = 3$$, $$select 2 where 4 = 4$$) as j;