我有一个批处理 SQL 基准查询,我想执行这些查询并计算它们的时间。我不想使用命令 EXECUTE ANALYZE 获取执行计划。
基于此处建议的方法 Get execution time of PostgreSQL query 我已经编写了一个程序,其中包含每个查询的以下代码
-------------------------
Query = 'Q1';
StartTime := clock_timestamp();
PERFORM <** Query 1 goes here **>
EndTime := clock_timestamp();
Duration := 1000 * ( extract(epoch from EndTime) - extract(epoch from StartTime) );
INSERT INTO execution_tests VALUES (Query, StartTime, EndTime, Duration);
-------------------------
我使用 PERFORM 而不是 SELECT 来执行查询并丢弃结果。
这适用于简单的查询,但我有很多查询都带有 WITH 子句。根据手册
For WITH queries, use PERFORM and then place the query in parentheses. (In this case, the query can only return one row.)
但是,我需要进行基准测试的所有 WITH 查询都会返回多行。
如何解决这个问题?
最佳答案
怎么样
do $$
declare
p json;
your_query text := 'select pg_sleep(1.1)';
t interval;
begin
execute 'explain (analyse, format json) ' || your_query into p;
raise info '%', p;
t := make_interval(secs := ((p->0->>'Planning Time')::float + (p->0->>'Execution Time')::float)/1000);
raise info '%', t;
end $$;
关于postgresql - 如何对一组 PostgreSQL 查询进行基准测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44508895/