将postrgresql EXPLAIN ANALIZE作为列

时间:2017-08-17 16:14:17

标签: sql postgresql

所以如果我在postgresql上执行EXPLAIN ANALIZE查询,我会得到这个结果:

查询计划

Seq Scan on yellow_tripdata_staging (cost=0.00..3686590.00 rows=67978653 width=198) (actual time=0.009..307340.447 rows=92987719 loops=1)
Filter: ((passenger_count)::text = '1'::text)
Rows Removed by Filter: 38177324
Planning time: 0.306 ms
Execution time: 319613.033 ms

查询计划是列名,其他是行,是否有办法获取每一行都是列的数据?

类似的东西:

过滤器|删除行|计划时间|执行时间|

东西| 38177324 | .0306 ms | 319613.03 ms |

1 个答案:

答案 0 :(得分:3)

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;
┌─────────────────────────────────────┬───────────┬───────────┬────────────┐
│                  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        ↵│           │           │            │
│   }                                ↵│           │           │            │
│ ]                                   │           │           │            │
└─────────────────────────────────────┴───────────┴───────────┴────────────┘

链接:
EXPLAIN
JSON operators

<强>更新

获取查询集的计划集,这是几种可能的解决方案之一:

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;

或使用variadic参数(几乎与之前相同):

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;