Re: can stored procedures with computational sql queries improve API performance?

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: Krishnakant Mane <kkproghub(at)gmail(dot)com>
Cc: Guyren Howe <guyren(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: can stored procedures with computational sql queries improve API performance?
Date: 2024-07-10 13:13:09
Message-ID: B33971DD-9219-4C76-8DA1-911F416F10EE@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On Jul 9, 2024, at 7:21 PM, Krishnakant Mane <kkproghub(at)gmail(dot)com> wrote:
>
> 
>> On 7/10/24 06:44, Guyren Howe wrote:
>>> On Jul 9, 2024, at 17:58, Krishnakant Mane <kkproghub(at)gmail(dot)com> wrote:
>>> Hello.
>>>
>>> I have a straight forward question, but I am just trying to analyze the specifics.
>>>
>>> So I have a set of queries depending on each other in a sequence to compute some results for generating financial report.
>>>
>>> It involves summing up some amounts from tuns or of rows and also on certain conditions it categorizes the amounts into types (aka Debit Balance, Credit balance etc).
>>>
>>> There are at least 6 queries in this sequence and apart from 4 input parameters. these queries never change.
>>>
>>> So will I get any performance benefit by having them in a stored procedure rather than sending the queries from my Python based API?
>> Almost certainly.
>>
>> Doing it all in a stored procedure or likely even better a single query will remove all of the latency involved in going back and forth between your app and the database.
>>
>> Insofar as the queries you are running separately access similar data, a single query will be able to do that work once.
>>
>> There are other potential benefits (a smaller number of queries reduces planning time, for example).
>
>
> Basically there are if else conditions and it's not just the queries but the conditional sequence in which they execute.
>
> So one single query won't do the job.
>
Are you processing the results of each of the queries in your python code before sending the next query? If so, i don't think you will see much improvement per query

> But Thank you for confirming my understanding.
>
> I believe that the fact that stored procedures are compiled also makes them perform faster, is that correct?
>
If the SP is fired in a loop or very frequently ( not monthly), yes
> Regards.
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2024-07-10 13:32:16 Re: Finding error in long input file
Previous Message Hans Schou 2024-07-10 13:03:39 Re: Finding error in long input file