postgresql:相同的原始查询比在函数中运行得更快

eit6fx6z  于 2022-12-12  发布在  PostgreSQL
关注(0)|答案(1)|浏览(131)

同样的查询在函数外运行得更快,但在函数中运行得慢了几百倍,当我删除where子句时,它在函数中运行得也更快,但并不比原始查询快。

RETURN QUERY (SELECT 
    t.tran_id,  t.tran_date,  t.value_date, t.statement_reference, t.debit,   t.credit,t.office_id
FROM transactions.transactions_view t
WHERE t.tran_date BETWEEN from_::date AND to_::date

 AND t.gl_account_id = gl_account_id_);
yhxst69z

yhxst69z1#

You should try with:

RETURN QUERY EXECUTE (SELECT 
    t.tran_id,  t.tran_date,  t.value_date, t.statement_reference, t.debit, t.credit,t.office_id
FROM transactions.transactions_view t
WHERE t.tran_date BETWEEN from_::date AND to_::date
 AND t.gl_account_id = gl_account_id_);

See RETURN QUERY vs RETURN QUERY EXECUTE and PostgreSQL: Documentation

相关问题