带有函数包装PostgreSQL的sql这么慢? [英] postgresql with function wrap sql so slow?

查看:258
本文介绍了带有函数包装PostgreSQL的sql这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个sql解释分析:

first sql explain analyse:

explain  analyse  select * from ttq.ttq_post;
Seq Scan on ttq_post  (cost=10000000000.00..10000000014.71 rows=171 width=547) (actual time=0.005..0.027 rows=176 loops=1)
Planning Time: 0.033 ms
Execution Time: 0.041 ms

但是如果使用函数包装相同的sql

例如:

but if use function wrap the same sql
eg:

create or replace function ttq.test_fn_slow() 
  returns  setof ttq.ttq_post 
  language  sql 
  stable 
as $$
select * from ttq.ttq_post;
$$

和执行打击功能:

explain  analyse  select ttq.test_fn_slow();

结果:

ProjectSet  (cost=0.00..5.27 rows=1000 width=32) (actual time=0.063..0.175 rows=176 loops=1)
  ->  Result  (cost=0.00..0.01 rows=1 width=0) (actual time=0.001..0.001 rows=1 loops=1)
Planning Time: 0.013 ms
Execution Time: 0.192 ms

为什么使用函数包装这么慢?

why use function wrap so slow?

尝试使用不可变替换稳定,但结果相同!

try use "immutable" replace "stable" but the result is same!

推荐答案

额外的费用一定是由于您在 SELECT 子句而不是 FROM中使用了set returning函数子句。

The additional cost must be due to the fact that you are using the set returning function in the SELECT clause rather than in the FROM clause.

请注意, SELECT 子句中对返回集合函数的处理已更改在PostgreSQL v10中,因此您的版本可能会影响这种行为。

Note that the processing of set returning functions in the SELECT clause changed in PostgreSQL v10, so your version may have an influence on this behavior.

这篇关于带有函数包装PostgreSQL的sql这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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