【问题标题】:get postrgresql EXPLAIN ANALIZE as columns获取 postgresql EXPLAIN ANALYZE 作为列
【发布时间】:2017-08-17 16:14:17
【问题描述】:

所以如果我在 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 毫秒 |319613.03 毫秒|

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:
    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 │ │ { ↵│ │ │ │ │ “计划”:{ ↵│ │ │ │ │ “节点类型”:“结果”,↵│ │ │ │ │ “平行感知”:假,↵│ │ │ │ │ “启动成本”:0.00, ↵│ │ │ │ │ “总成本”:0.01, ↵│ │ │ │ │ “计划行”:1,↵│ │ │ │ │ “平面宽度”:4,↵│ │ │ │ │“实际启动时间”:0.002,↵│ │ │ │ │“实际总时间”:0.002,↵│ │ │ │ │ “实际行数”:0, ↵│ │ │ │ │ “实际循环”:1,↵│ │ │ │ │ "输出": ["1"], ↵│ │ │ │ │ "一次性过滤器": "false" ↵│ │ │ │ │ }, ↵│ │ │ │ │ “计划时间”:0.042,↵│ │ │ │ │ “触发器”:[ ↵│ │ │ │ │ ], ↵│ │ │ │ │ “执行时间”: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;
    

    【讨论】:

    • 谢谢,这正是我所需要的,你的一个问题:是否有可能获得多个查询的解释?喜欢,多行这个解释?
    • @FernandoCastillaOspina 你的意思是像get_explain_as_json(query1, query2, ...) 这样的东西,它为每个查询单独返回几行?
    • 回顾过去,这似乎很明显,不知道我是怎么错过的。谢谢@Abelisto :)
    • 你确定它应该有效吗?我添加了另一个查询,例如get_explain_as_json('select 1 where 2 = 3;select 2 where 2= 4') as j;,但随后数据库显示没有结果
    • @FernandoCastillaOspina 抱歉,根据您的回复,我认为您自己找到了解决方案。答案已更新(但未经测试)。
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多