Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNIfB-0005SW-B4 for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 17:11:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNIf9-0004UI-TI for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 17:11:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNIf9-0004UB-KQ for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 17:11:51 +0000 Received: from mout.gmx.net ([212.227.15.19]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNIf6-0006US-J0 for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 17:11:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1616173907; bh=UH+kDbtlZKmop1tx9niBXvfX2YQXTD8EUKKYdZbwBIc=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=ZugClwVooH/YojTG1ac+RTroXaAN3M/8DwUsR4dfumWTCD6/QErCmciEw5yE3X6tr pzsmTYX5I7tKxPiejqtYpsaoOiZP4qsg7ZX9oJse19w4jBmSFpynFjSV6Ca2OzBLcn Sxo9pUKPe9z5BTuWC6NVJ71koEc7xTdMC6grzG74= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.20] ([83.171.168.156]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1M9o1v-1lI3rb3N7V-005mAu for ; Fri, 19 Mar 2021 18:11:46 +0100 Subject: Re: SELECT is faster on SQL Server To: pgsql-general@lists.postgresql.org References: <33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net> From: Thomas Kellerer Message-ID: <47da8273-23dd-789a-ca7d-a5b803d9010f@gmx.net> Date: Fri, 19 Mar 2021 18:11:45 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:ERnajsuNG+A7SRH6icyNndZt7v8qzicDLeFP+sKuA3sNxoLQYyM zJOguU3ZdJAkS6SiRMd2hrSi7b/qyxHh4v+h/luZ0ay1VbLCxkat0rLrEkFlHJIkHu/BauA rg7j77Gg1Uz7HjPtkGt4rkgqFV4+6zHwNXixizvQNB+GRpKF5m9lIn+nTAPJP9Uop6nghb7 WbG8/yq01JI70DJRe4TZA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:TPnRL2/9YS4=:b0qRnTfxYACspWZr3hLS/H 2oEGiMJXysmPhHdcTf69XZw29L7Y1Z4ySuHV0wbnlbznwUXP167CRs+Z1R0RrWtcDWsrCrjuL OVzb+LXIATa3mxeWmfctLXXKQbezhkrOH93GnsDHiz0z3zEMIm9o0qFpsTm3smAOnH8WMN4VB wndombfRk+4hg9uc5KEiBYsMQxnAIggGtePQ8bEoAsp+oZLeqeZyBW9teHroVuAt+mJfqs4bo mKL+sjrI3JPO+9w+4ZwSXv00OW9xS8dVqbyuWruqRJ7zQ83kmbf0m4K5LLTSQk5A8O422+czM 351HYy5E25/hvCKtsHn7qoGoL6r2SS+z7HxsBOaNsoeHZ5y+KgiO6OJVByuXwgbxzS7fmvy8u /K3DCv4ioRUvEIgRUdxaQJz8RArJ4m16Qst9NdMX0W1DbaHC+O/MdfaDy7pvC6kmx/SYxwgDu K/6TgLyELDlz7D7Tfgg4WeQCG6NL/B2uKghnxkM4CwJsMXDb5IJ3+Afg6r4DH2V6PIN2VzW+M Wj5jv6/BBW6CMd6tKPnBcIP2eUfcdBfg4P9T/SIFd2ROV1PU7AzAZGDKc+/9SI/eYP9aBiUp8 zxZfflOzfihRGjLhb72MsuQJ+1PQPqF69Ca0Ij38wHSzYMGU6NZIXgy1ZiWSaT/4pI+F78li4 kW8iqCOVij1KpQ1NGklRpYYYDqUdNSW6MVMRwLx0pRAqsvNPGVXL1aUt23ZQFNSc2xCIeJ8A0 JqKC/ravxol7tBL2xNrZeGXoR40NEQ2inyaVNwG1HTLGjBN1/XUP4dIJKNUjWdX177+Wv6cr8 dnA57vs0A5fccWmoQr13qOnC/EEENKOhuUz3XZUie9LHf5TlarQEpitHd9KE28gadHXwMNHel 4caFOvcYGbWNHZiOwgCQ== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Frank Millman schrieb am 19.03.2021 um 10:16: >>>> Very often "distinct on ()" is faster in Postgres compared to the equ= ivalent solution using window functions >> The two derived tables (cl_bal, op_bal) seem to be doing exactly the sa= me thing - at least I can't spot a difference. >> >> If that is correct, you can move them into a common table expression - = maybe detecting that is SQL Server's secret sauce. >> > There is a difference. > > cl_bal selects WHERE tran_date <=3D '2018-03-31'. > > op_bal selects WHERE tran_date < '2018-03-01'. > > The second one could be written as WHERE tran_date <=3D '2018-02-28', bu= t I don't think that would make any difference. I knew I overlooked something ;) But as one is a true subset of the other, I think you can merge that into = a single SELECT statement: select '2018-03-01' AS op_date, '2018-03-31' AS cl_date, a.source_code_id, sum(a.tran_tot) AS cl_tot, sum(a.tran_tot) filter (where tran_date < '2018-03-01') AS op_= tot FROM ( SELECT distinct on (location_row_id, function_row_id, source_code_= id) source_code_id, tran_tot, tran_date FROM prop.ar_totals WHERE deleted_id =3D 0 AND tran_date <=3D '2018-03-31' AND ledger_row_id =3D 1 ORDER BY location_row_id, function_row_id, source_code_id, tran_da= te DESC ) AS a GROUP BY a.source_code_id