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 1lNBG2-00048j-Ck for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 09:17:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNBG1-0001HE-1r for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 09:17:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNBG0-0001Gk-QB for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 09:17:24 +0000 Received: from securemail-y45.synaq.com ([196.35.198.108]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNBFx-0000Je-Qr for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 09:17:23 +0000 Authentication-Results: synaq.com; iprev=fail smtp.remote-ip=197.90.32.26; auth=pass (PLAIN) smtp.auth=frank@chagford.com; arc=none Received: from [197.90.32.26] (helo=[192.168.0.8]) by securemail-pl-omx11.synaq.com with esmtpa (Exim 4.93.0.4-30-5edf9c7) (envelope-from ) id 1lNBFV-000aTB-6I for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 11:16:53 +0200 Subject: Re: SELECT is faster on SQL Server To: pgsql-general@lists.postgresql.org References: <33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net> From: Frank Millman Message-ID: Date: Fri, 19 Mar 2021 11:16:50 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-SYNAQ-Pinpoint-Information: Please contact SYNAQ for more information X-SYNAQ-Pinpoint-ID: 1lNBFV-000aTB-6I X-SYNAQ-Pinpoint: No virus infections found X-SYNAQ-Pinpoint-SpamCheck: not spam, SpamAssassin (not cached, score=-1.801, required 9, autolearn=disabled, ALL_TRUSTED -1.00, DCC_REPUT_00_12 -0.80, NICE_REPLY_A -0.00) X-Pinpoint-From: frank@chagford.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2021-03-19 11:04 AM, Thomas Kellerer wrote: > Frank Millman schrieb am 19.03.2021 um 09:52: >>>> I am writing a cross-platform accounting app, and I test using Sql >>>> Server on Windows 10 and PostgreSql on Fedora 31. Performance is >>>> usually very similar, with a slight edge to PostgreSql. Now I have a >>>> SELECT which runs over twice as fast on Sql Server compared to >>>> PostgreSql. >>>> >>> Can you change the SELECT statement? >>> >>> Very often "distinct on ()" is faster in Postgres compared to the equivalent solution using window functions >> Thanks, Thomas >> >> I tried that, and it ran about 10% faster. Every little helps, but SQL Server appears to have some secret sauce! > The two derived tables (cl_bal, op_bal) seem to be doing exactly the same 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. > > with totals as ( > SELECT a.source_code_id, SUM(a.tran_tot) AS total > FROM ( > SELECT distinct on (location_row_id, function_row_id, source_code_id) source_code_id, tran_tot > FROM prop.ar_totals > WHERE deleted_id = 0 > AND tran_date <= '2018-03-31' > AND ledger_row_id = 1 > ORDER BY location_row_id, function_row_id, source_code_id, tran_date DESC > ) AS a > GROUP BY a.source_code_id > ) > select > '2018-03-01' AS op_date, '2018-03-31' AS cl_date, > cl_bal.source_code_id, op_bal.total as op_tot, cl_bal.total.cl_tot > FROM totals as cl_bal > LEFT JOIN totals as op_bal ON op_bal.source_code_id = cl_bal.source_code_id; > There is a difference. cl_bal selects WHERE tran_date <= '2018-03-31'. op_bal selects WHERE tran_date < '2018-03-01'. The second one could be written as WHERE tran_date <= '2018-02-28', but I don't think that would make any difference. Frank