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 1lNB3P-0003kV-6f for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 09:04:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNB3N-0007r1-0T for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 09:04:21 +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 1lNB3M-0007qu-P0 for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 09:04:20 +0000 Received: from mout.gmx.net ([212.227.17.22]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNB3G-0000DS-9U for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 09:04:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1616144651; bh=tl2ovFKj5nb3qOh6PFwwX2bbROU05FeIYK/0/huNl8s=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=QP40lLEgkp+aNqMB92zu3V/wyyZme+9mTFQrv73yiD2lwaXHCN2lvh2q7F8KemsDT Kco8snrrQuryxZ7+EdFOK0DFddIzIYQMvFgtKdMeI4ebK1VJYUOcKSI5YvKDUUp2c+ noBmOhB1ohabqND8XvxqFCRUDhuFHOzZyKIvFoQE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [10.1.32.157] ([185.40.248.10]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MS3mt-1lHIBc2YGJ-00TRsf for ; Fri, 19 Mar 2021 10:04:11 +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: Date: Fri, 19 Mar 2021 10:04:10 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:VmQQoXddNwOX8XHRF/q2yQYFO8cW2AHK6ep7f7OXdeS1i1Ue9Xb t89sEysRDwyz5k0BYjpC1EcpQXoCs5vBEfLUp0VLOC04aa/EoI8+pZQt+0bhUQHC8oQddyR ts8kRskrxAf57qWAR91IaB/purXS7WzQUdVPAAhqVgEmmhdgSAiQsLxBzsm0bxu0Jfn1vH9 DBuffFuoCpcaUiGEBnm+Q== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:+40OJlrQlOc=:BGkV6KvTVoXLbm8uyHwnFW mG48KtyvfB3LlWZxQVoid+qCMNxdxdmdoJkn/D7DABtwg6fR17TZU0I92TqdKD2KvgrRJjjsD mqc+nwUza/K3WmmT8P4Eb0mUr0t8sTnMUYI6qu0bx7x9Y6yiVXvUDQ9oT+dnv0/rfrYbgJPcd cVdr+7dlBxNr5y88ZeihODuv6bf2zpGF1Mxd+JSr04E7xPJE0sdQ4ApSel53NjW//JbCSzq7S NiifpLBcUpuUEjDyss1kjVAY+8g7aZOzVaTHZ+JMM4y17VGtVLpvUem8WQrqmHXBNZ6ld+Ijc AJs/2aguG3eIOX1H0KehAEsSPmZModJqmNhEvI5ITWpnxEOEHN6VsuiUbRi9/8mc5YZ9eOmfH d5AnAc1e8i2DFB8tOLzU7jUlvW2tO5RCd6Z1sHJy1M/vdacFEJAPqw0HiWOjvYuyM40ZOExYU ZQeMoq5SbUw6tTXJyttMr2n47OQa9P7BBLcMrmil2NbUDTm6N8m04Kae7QPGbGmQb6tWN6vDF XV9K11jCcgGbQLmsGvhnoN3SqMnanb7YjVA42Q34YPgd+ybVU1AuwP5dQUIZ/6EKQn1yzRsj3 xR7dw2ApQkKmAqewYxt96ZrVsqVaJbIxuOomps4CmmH8K6RmUT1TQ8/qOLtpr9VcNjegYwo8U Mp5b7QjdaGvfYhVV8ofqJCAlFfUrMzGkk+Ley9wFiYtS73GeXMgeWYE0fDTx9CO0lwX2jdIrJ mRZkPEn4niqO5eCS1W94iaPUF4uP20ZVvvETMyjBzruMF1Cqd2x3403eXaA9rRhkBfsA00mCQ j6sV4SyKQ2AGxtj8SAU1AqJFUdoECtyEv3o7gxTK38acPRxQm8oiwZlAf32hCnSkPu878jDuc wWgWNbjAShtYI76NzFjg== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 equiv= alent solution using window functions > > Thanks, Thomas > > I tried that, and it ran about 10% faster. Every little helps, but SQL S= erver 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 - may= be 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_co= de_id) source_code_id, tran_tot 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= _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 =3D cl_bal.sourc= e_code_id;