循环postgresql中的计时

timing within a loop postgresql

我在 postgres 中创建了一个函数,它在循环内有一个查询,该循环迭代 n 次。我想知道每次迭代的执行时间。 (这里给出的是类似的函数)

'\timing' 将给出函数调用执行时间,但不完全是循环迭代时间。

create function somefunction()
return something
---
Declare
--
--
BEGIN
for somevalue in something
loop 
insert into table_name
select name,id from sales where name = "John"; --want to know the execution time of this statement
end loop;
return 
END
$$ language plpgsql volatile;

我还尝试在循环中的插入语句之前放置 EXPLAIN ANALYZE(如下所示)

loop
EXPLAIN ANALYZE insert into table_name
select name,id from sales where name = "John";
end loop;

出现此错误 - 错误:查询没有结果数据的目的地

您可能希望将 EXPLAIN ANALYZE 的结果存储到记录或字符可变变量中,以便稍后使用该变量查看结果。

像这样:

EXPLAIN ANALYZE insert into table_name 
    select name,id from sales where name = "John" INTO result;

然后您现在可以使用该变量来查看执行时间,方法是使用 RAISE NOTICE 或将其插入新的 table 或任何您喜欢的