postgresql – 将EXPLAIN结果放入表中?

postgresql – 将EXPLAIN结果放入表中?,第1张

概述我从 Postgres 8.1 docs看到EXPLAIN生成了类似于表格数据: Prior to PostgreSQL 7.3, the plan was emitted in the form of a NOTICE message. Now it appears as a query result (formatted like a table with a single text colu 我从 Postgres 8.1 docs看到EXPLAIN生成了类似于表格数据:

Prior to Postgresql 7.3,the plan was emitted in the form of a NOTICE
message. Now it appears as a query result (formatted like a table with
a single text column).

我正在使用9.0,为此,docs说,输出可以是各种类型,包括TEXT和XML.我真正想做的是将输出视为标准查询结果,以便为查询或一组查询生成简单的报告,例如,

SELECT maxcost FROM (    EXPLAIN VERBOSE     SELECT COUNT(*)       FROM Mytable     WHERE value>17);

上面的内容不能用于我尝试过的任何形式,并且我编写了属性maxcost来演示抽取特定数据位(在这种情况下,查询的最大估计成本)是多么简洁.我能做些什么会让我成为那里的一部分吗?我宁愿能够在一个简单的sql控制台中工作.

解决方法 到目前为止还没有其他答案,所以这是我自己的努力.

可以将解释结果读入plpgsql中的变量,并且由于输出可以是XML,因此可以将EXPLAIN包装在存储函数中,以使用xpath产生顶级成本:

CREATE OR REPLACE FUNCTION estimate_cost(IN query text,OUT startup numeric,OUT totalcost numeric,OUT planrows numeric,OUT planwIDth numeric)AS$BODY$DECLARE    query_explain  text;    explanation       xml;    nsarray text[][];BEGINnsarray := ARRAY[ARRAY['x','http://www.postgresql.org/2009/explain']];query_explain :=e'EXPLAIN(FORMAT XML) ' || query;EXECUTE query_explain INTO explanation;startup := (xpath('/x:explain/x:query/x:Plan/x:Startup-Cost/text()',explanation,nsarray))[1];totalcost := (xpath('/x:explain/x:query/x:Plan/x:Total-Cost/text()',nsarray))[1];planrows := (xpath('/x:explain/x:query/x:Plan/x:Plan-Rows/text()',nsarray))[1];planwIDth := (xpath('/x:explain/x:query/x:Plan/x:Plan-WIDth/text()',nsarray))[1];RETURN;END;$BODY$LANGUAGE plpgsql;

因此,问题的例子变成:

SELECT totalcostFROM estimate_cost('SELECT COUNT(*)       FROM Mytable     WHERE value>17');
总结

以上是内存溢出为你收集整理的postgresql – 将EXPLAIN结果放入表中?全部内容,希望文章能够帮你解决postgresql – 将EXPLAIN结果放入表中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1157737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存