Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sRX8S-00GBPc-T0 for pgsql-general@arkaria.postgresql.org; Wed, 10 Jul 2024 13:13:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sRX8Q-0054ZR-Ch for pgsql-general@arkaria.postgresql.org; Wed, 10 Jul 2024 13:13:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sRX8Q-0054ZJ-1H for pgsql-general@lists.postgresql.org; Wed, 10 Jul 2024 13:13:26 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sRX8N-001OUd-Vs for pgsql-general@lists.postgresql.org; Wed, 10 Jul 2024 13:13:25 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1fb53bfb6easo30606625ad.2 for ; Wed, 10 Jul 2024 06:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720617201; x=1721222001; darn=lists.postgresql.org; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=uEBRdKc+IHa6Sv6EU63HVN9fVFmF/MyolZmNfKYNiMo=; b=SCmH1NX6/E/oKUJElak85Mm2SKRtmNqoG2roifIElo7OwgIRJxjp84BQOLentMVtW6 VFnDozoSrHRaEXIvwi9ymFY0FVE6fCM3e9MRem5BefssuYpincgVtjvJ6cO+TpYXMJfC F4y/2RTA32RTfJ7ow+V9JdJm0Y3ru7gSsI0qGD2MfUOKk2NpVhW9CaL5DjRJeXPBo3Ez IR7AR+H5vX3kul2ejT6nADTyXKSA3CnNHznYD9rjyDcTtdM/H2yRX9uc/l8sQmud8mmI bgDDO2g5E0GklsHu8A03q1+ixWdMUcGCAPbewM9rdVVOZiaIL0laLpgWFEnGDqyu45Qt u9fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720617201; x=1721222001; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uEBRdKc+IHa6Sv6EU63HVN9fVFmF/MyolZmNfKYNiMo=; b=EAEINcpUrNKNPbVUlNlFO/IPEK9jjsEfl8q/9eDe6dMnN9eRT1gHyLreoGybsjA1uL ggZwYXP+OreOi2UGUjPXkekeMVdOGA8gV+zqIccK9enploxZuJr2oRWqob1y55T5R9Cx TlTxiJ1vluA9u7UKan3s6ep0QWgCtkweL75OV96/0PI6Y+Wnv95TIsSyyqqYE0F3GWPY PhD3Nt6Z/9VT3/jmhM4kR+QvEKKhZcdataTl8rQNM2Wtp5UoGdETEeLlsvjaLSkUz6ev jMoj5sglqYu4b9/t+PVUX7w5EKr1P5WlUrbcS9I90D70Ena8MIyuMTp8TpIau12GvSpO QRCg== X-Forwarded-Encrypted: i=1; AJvYcCWERgiDGir2RI5BmjS2BtYOl2bt5797RH5fbsM+09gr3pJn6jyWcp3JqZsS1RgH0kGyiLoYSEg4MsMtQQLl9VATOlOFHj+HwOh2WNgog/tCXaVY X-Gm-Message-State: AOJu0Yy0nhGw0f/350k02D6kn5ZBTCw9/aXjim7AHzGINp4EpuG49W6l osLBQhh7Sr/i8esjuZa0DgAnkoYRU/erofEADHkOl7vajMEkH+zE39hdsw== X-Google-Smtp-Source: AGHT+IHPiO8O8pvKK7OSAP89LXIgNyCAszA6EP6zZX30q+xpgM1K24QG5tAKh3NEy8RJdtPgmzo1uA== X-Received: by 2002:a17:902:d2ca:b0:1fb:58b8:2fbc with SMTP id d9443c01a7336-1fbb6d2a37dmr48375205ad.29.1720617201133; Wed, 10 Jul 2024 06:13:21 -0700 (PDT) Received: from smtpclient.apple ([2601:681:5a00:8a0:34fe:2028:a3b6:f181]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fbb6ac08dfsm33146495ad.213.2024.07.10.06.13.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 10 Jul 2024 06:13:20 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Rob Sargent Mime-Version: 1.0 (1.0) Subject: Re: can stored procedures with computational sql queries improve API performance? Date: Wed, 10 Jul 2024 07:13:09 -0600 Message-Id: References: Cc: Guyren Howe , pgsql-general@lists.postgresql.org In-Reply-To: To: Krishnakant Mane X-Mailer: iPhone Mail (21F90) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Jul 9, 2024, at 7:21=E2=80=AFPM, Krishnakant Mane = wrote: >=20 > =EF=BB=BF >> On 7/10/24 06:44, Guyren Howe wrote: >>> On Jul 9, 2024, at 17:58, Krishnakant Mane wrote: >>> Hello. >>>=20 >>> I have a straight forward question, but I am just trying to analyze the s= pecifics. >>>=20 >>> So I have a set of queries depending on each other in a sequence to comp= ute some results for generating financial report. >>>=20 >>> It involves summing up some amounts from tuns or of rows and also on cer= tain conditions it categorizes the amounts into types (aka Debit Balance, Cr= edit balance etc). >>>=20 >>> There are at least 6 queries in this sequence and apart from 4 input par= ameters. these queries never change. >>>=20 >>> So will I get any performance benefit by having them in a stored procedu= re rather than sending the queries from my Python based API? >> Almost certainly. >>=20 >> Doing it all in a stored procedure or likely even better a single query w= ill remove all of the latency involved in going back and forth between your a= pp and the database. >>=20 >> Insofar as the queries you are running separately access similar data, a s= ingle query will be able to do that work once. >>=20 >> There are other potential benefits (a smaller number of queries reduces p= lanning time, for example). >=20 >=20 > Basically there are if else conditions and it's not just the queries but t= he conditional sequence in which they execute. >=20 > So one single query won't do the job. >=20 Are you processing the results of each of the queries in your python code be= fore sending the next query? If so, i don't think you will see much improve= ment per query=20 > But Thank you for confirming my understanding. >=20 > I believe that the fact that stored procedures are compiled also makes the= m perform faster, is that correct? >=20 If the SP is fired in a loop or very frequently ( not monthly), yes=20 > Regards. >=20 >=20 >=20