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 1sRNEO-00ElWo-VU for pgsql-general@arkaria.postgresql.org; Wed, 10 Jul 2024 02:38:56 +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 1sRNEN-001WIE-8J for pgsql-general@arkaria.postgresql.org; Wed, 10 Jul 2024 02:38:55 +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 1sRNEM-001WI6-Qx for pgsql-general@lists.postgresql.org; Wed, 10 Jul 2024 02:38:54 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sRNEK-001Jlo-4c for pgsql-general@lists.postgresql.org; Wed, 10 Jul 2024 02:38:54 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1fb3cf78fcaso36218685ad.1 for ; Tue, 09 Jul 2024 19:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720579130; x=1721183930; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=eCGzH+35q8H4MpOc8l1TZTvQTUbOBlDRWD6SS15TxmE=; b=ef7OcqbrmW0BM0jQaZxxjlqUpb605twyVxGnGnBKmQVbfQllX7lBf6Aym6/CNfWSkL xkcj+vHNVR11v472Y3RXkE+0Nw2c3VZIdFVbb099mxxG2LZUix1rPtcfsgMgecI/bMHW hLQ135VeO9hSCUEAUi82nV+qsXZq0IJOkeTuCVQlksTT1Infc+P0NBRWkOFvwBgjknVp WJ8jaLU1Bsf4A0KbQv30Xf6oZyPUIxGg1p1cdaj8grUzI6qMQJnR9EXgx1BMdiQRyE9O qBDuaekhqhFF2KN5p3FelU/yZQyouFfkV0rePxkPmJIB2B1tXvGSBSis0bIHSeANV1je 4zKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720579130; x=1721183930; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eCGzH+35q8H4MpOc8l1TZTvQTUbOBlDRWD6SS15TxmE=; b=SsCpK65xYGo7RR/NcEb63oPvgMwW21B5BNwFK1cwqEtiPbI+tz1Rrf/Nlz4Z9xNAqx EVgl9NmyORbWh62BUi2qBTFBRnzgBA7r1DD0aHG77iK62ZlH2067X5RCaYCpzLWGARh7 Ug+KJ2wRwGFi/YcjcqYsUuO5qLxjn6LNcZ+ZRkTCRxf38a+9fyxvKiFcHiGOSFixHRM8 CeWjLNNVrhnEKFdiOnnmzsZ/HgBHxSd5yEtw8cByS9+Sjm9zrsFak1z+vIl1Xs0K2yBj WV/7r5+D6t9cIa0aOL1ib+zJnwQ+QpbhmuKtbOOIJZLx9b+JtN6W9FVncRt5U/663NF9 CNwA== X-Gm-Message-State: AOJu0YxIEMi/yt1O88plRloQl1Ey2oalpaWEGLXUuiZPxAB/0kYo80sB ObD3Hbn8BUn2Z6D8F9VQ03Wf+lM00KH9rpdNLTgVUP4NA2iEhxtO X-Google-Smtp-Source: AGHT+IGKqJl+PbssjPfzyhvBY61bG8j13TkFpyZwbVbkcf2MNLXhNQLKBZSQ3f2VNfOKkl1mfPnFNg== X-Received: by 2002:a17:903:2bcb:b0:1fb:3b30:8cf0 with SMTP id d9443c01a7336-1fbb6ed504fmr35281365ad.51.1720579129625; Tue, 09 Jul 2024 19:38:49 -0700 (PDT) Received: from smtpclient.apple ([2600:8801:8600:3d2::12]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fbb6ab76aasm22646345ad.161.2024.07.09.19.38.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jul 2024 19:38:49 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: can stored procedures with computational sql queries improve API performance? From: Guyren Howe In-Reply-To: Date: Tue, 9 Jul 2024 19:38:37 -0700 Cc: "pgsql-general@lists.postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: References: <04AE80B8-1F00-4758-A498-C1C7C1FC7A31@gmail.com> To: Krishnakant Mane X-Mailer: Apple Mail (2.3774.600.62) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 specifics. >>>=20 >>> So I have a set of queries depending on each other in a sequence to = compute some results for generating financial report. >>>=20 >>> 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). >>>=20 >>> There are at least 6 queries in this sequence and apart from 4 input = parameters. these queries never change. >>>=20 >>> 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. >>=20 >> 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. >>=20 >> Insofar as the queries you are running separately access similar = data, a single query will be able to do that work once. >>=20 >> There are other potential benefits (a smaller number of queries = reduces planning time, for example). >=20 >=20 > Basically there are if else conditions and it's not just the queries = but the conditional sequence in which they execute. >=20 > So one single query won't do the job. You might be surprised what you can do in one query. Feel free to share.