对许多PostgreSQL查询进行基准测试 [英] Benchmarking many PostgreSQL queries

查看:171
本文介绍了对许多PostgreSQL查询进行基准测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在复杂的网页上,执行了数百个查询。我正在寻找一种基准测试这些查询的方法。
我尝试将 EXPLAIN ANALYZE 嵌入查询查询中。放入psql函数中,以选择每个查询的执行时间并进行比较。
,但是 EXPLAIN ANALYZE 似乎无法以任何方式嵌入。



是否存在另一种解决方案来比较某些查询的执行时间?



非常感谢

解决方案

嵌入没有问题 EXPLAIN 在PL / pgSQL中:

 创建或替换功能配置文件(
IN查询文本,
OUT total_cost双精度,
OUT运行时双精度
)返回记录
语言plpgsql严格为
$$ DECLARE
j json;
开始
执行‘EXPLAIN(ANALYZE,FORMAT JSON)’||查询INTO j;
total_cost:=(j-> 0->计划->>总成本)::双精度;
runtime:=(j-> 0->计划->>实际总时间)::双精度;
返回;
END; $$;

您可以使用以下示例:

  test => SELECT * FROM profile($$ SELECT * FROM large WHERE val ='mama'$$); 
┌──────────┬┬────────┐
│总计成本│运行时│
├──────── ────────────────┤
│14542.43│207.836│
└──────────┴ ─┘
(1行)

请勿在不受信任的查询中使用它,因为函数容易受到SQL注入的攻击。<​​/ p>

On a complex webpage more than hundred queries are executed. I search for a way to benchmark these queries. I tried to embed EXPLAIN ANALYZE into a query resp. into a psql function, to pick the execution time of every query and compare them. But EXPLAIN ANALYZE seems not to be embeddable in any way.

Is there another solution to compare the execution time of some queries?

Thanks a lot

解决方案

There is no problem with embedding EXPLAIN in PL/pgSQL:

CREATE OR REPLACE FUNCTION profile(
      IN query text,
      OUT total_cost double precision,
      OUT runtime double precision
   ) RETURNS record
  LANGUAGE plpgsql STRICT AS
$$DECLARE
   j json;
BEGIN
   EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || query INTO j;
   total_cost := (j->0->'Plan'->>'Total Cost')::double precision;
   runtime := (j->0->'Plan'->>'Actual Total Time')::double precision;
   RETURN;
END;$$;

You can use it for example as follows:

test=> SELECT * FROM profile($$SELECT * FROM large WHERE val = 'mama'$$);
┌────────────┬─────────┐
│ total_cost │ runtime │
├────────────┼─────────┤
│   14542.43 │ 207.836 │
└────────────┴─────────┘
(1 row)

Don't use it with untrusted queries, as the function is vulnerable to SQL injection.

这篇关于对许多PostgreSQL查询进行基准测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆