0

Same query run faster outside of function but in function it runs hundred times slower and when I remove where clause it runs faster in function too but not faster than raw query. I wonder whats the problem.

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_);

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

later when I was checking the query I found that there is problem with one date range when I pass the date between '2022-10-29' AND '2022-11-20' then it works fine and it generates result within 2 sec but when I change the date range between '2022-10-29' AND '2022-11-20' it takes more than 8 minutes to generate the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.