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 1lNBL0-0004IB-Qo for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 09:22:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNBKz-0007OC-OP for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 09:22:33 +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 1lNBKz-0007O4-DM for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 09:22:33 +0000 Received: from securemail-y6.synaq.com ([196.35.198.35]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNBKs-00028b-39 for pgsql-general@postgresql.org; Fri, 19 Mar 2021 09:22:32 +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-omx12.synaq.com with esmtpa (Exim 4.93.0.4-30-5edf9c7) (envelope-from ) id 1lNBKd-000glY-El for pgsql-general@postgresql.org; Fri, 19 Mar 2021 11:22:11 +0200 Subject: Re: SELECT is faster on SQL Server References: <33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net> To: Postgres General From: Frank Millman Message-ID: Date: Fri, 19 Mar 2021 11:22:06 +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: multipart/alternative; boundary="------------79D7BFB728F36F0C6BB1EDA4" Content-Language: en-US X-SYNAQ-Pinpoint-Information: Please contact SYNAQ for more information X-SYNAQ-Pinpoint-ID: 1lNBKd-000glY-El X-SYNAQ-Pinpoint: No virus infections found X-SYNAQ-Pinpoint-SpamCheck: not spam, SpamAssassin (not cached, score=-1.8, required 9, autolearn=disabled, ALL_TRUSTED -1.00, DCC_REPUT_00_12 -0.80, HTML_MESSAGE 0.00, 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 This is a multi-part message in MIME format. --------------79D7BFB728F36F0C6BB1EDA4 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2021-03-19 10:56 AM, Pavel Stehule wrote: > > > pá 19. 3. 2021 v 9:53 odesílatel Frank Millman > napsal: > > > On 2021-03-19 10:29 AM, Thomas Kellerer wrote: > > Frank Millman schrieb am 19.03.2021 um 09:19: > >> This may be a non-issue, and I don't want to waste your time. > But perhaps someone can have a look to see if there is anything > obvious I have missed. > >> > >> 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 > > > > Something along the lines (for the first derived table): > > > > SELECT ... > > FROM ( > >      SELECT a.source_code_id, SUM(a.tran_tot) AS cl_tot > >      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 > > ) as cl_bal > > ... > Thanks, Thomas > > I tried that, and it ran about 10% faster. Every little helps, but > SQL > Server appears to have some secret sauce! > > > can you send a result of EXPLAIN ANALYZE? > > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------------------------------------------ >  Nested Loop Left Join  (cost=5.66..5.74 rows=1 width=132) (actual > time=0.213..0.248 rows=5 loops=1) >    Join Filter: (a_1.source_code_id = a.source_code_id) >    Rows Removed by Join Filter: 4 >    ->  GroupAggregate  (cost=3.65..3.67 rows=1 width=36) (actual > time=0.144..0.157 rows=5 loops=1) >          Group Key: a.source_code_id >          ->  Sort  (cost=3.65..3.65 rows=1 width=10) (actual > time=0.131..0.135 rows=29 loops=1) >                Sort Key: a.source_code_id >                Sort Method: quicksort  Memory: 26kB >                ->  Subquery Scan on a  (cost=2.36..3.64 rows=1 > width=10) (actual time=0.063..0.116 rows=29 loops=1) >                      Filter: (a.row_num = 1) >                      Rows Removed by Filter: 3 >                      ->  WindowAgg  (cost=2.36..3.24 rows=32 width=34) > (actual time=0.062..0.107 rows=32 loops=1) >                            ->  Sort  (cost=2.36..2.44 rows=32 > width=26) (actual time=0.054..0.059 rows=32 loops=1) >                                  Sort Key: ar_totals.location_row_id, > ar_totals.function_row_id, ar_totals.source_code_id, > ar_totals.tran_date DESC >                                  Sort Method: quicksort Memory: 27kB >                                  ->  Seq Scan on ar_totals > (cost=0.00..1.56 rows=32 width=26) (actual time=0.014..0.028 rows=32 > loops=1) >                                        Filter: ((tran_date <= > '2018-03-31'::date) AND (deleted_id = 0) AND (ledger_row_id = 1)) >    ->  GroupAggregate  (cost=2.01..2.03 rows=1 width=36) (actual > time=0.017..0.017 rows=1 loops=5) >          Group Key: a_1.source_code_id >          ->  Sort  (cost=2.01..2.02 rows=1 width=10) (actual > time=0.012..0.013 rows=8 loops=5) >                Sort Key: a_1.source_code_id >                Sort Method: quicksort  Memory: 25kB >                ->  Subquery Scan on a_1  (cost=1.68..2.00 rows=1 > width=10) (actual time=0.032..0.047 rows=8 loops=1) >                      Filter: (a_1.row_num = 1) >                      ->  WindowAgg  (cost=1.68..1.90 rows=8 width=34) > (actual time=0.031..0.043 rows=8 loops=1) >                            ->  Sort  (cost=1.68..1.70 rows=8 width=26) > (actual time=0.023..0.024 rows=8 loops=1) >                                  Sort Key: > ar_totals_1.location_row_id, ar_totals_1.function_row_id, > ar_totals_1.source_code_id, ar_totals_1.tran_date DESC >                                  Sort Method: quicksort Memory: 25kB >                                  ->  Seq Scan on ar_totals > ar_totals_1  (cost=0.00..1.56 rows=8 width=26) (actual > time=0.006..0.013 rows=8 loops=1) >                                        Filter: ((tran_date < > '2018-03-01'::date) AND (deleted_id = 0) AND (ledger_row_id = 1)) >                                        Rows Removed by Filter: 24 >  Planning Time: 0.479 ms >  Execution Time: 0.344 ms > (33 rows) > > > --------------79D7BFB728F36F0C6BB1EDA4 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


On 2021-03-19 10:56 AM, Pavel Stehule wrote:


pá 19. 3. 2021 v 9:53 odesílatel Frank Millman <frank@chagford.com> napsal:

On 2021-03-19 10:29 AM, Thomas Kellerer wrote:
> Frank Millman schrieb am 19.03.2021 um 09:19:
>> This may be a non-issue, and I don't want to waste your time. But perhaps someone can have a look to see if there is anything obvious I have missed.
>>
>> 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
>
> Something along the lines (for the first derived table):
>
> SELECT ...
> FROM (
>      SELECT a.source_code_id, SUM(a.tran_tot) AS cl_tot
>      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
> ) as cl_bal
> ...
Thanks, Thomas

I tried that, and it ran about 10% faster. Every little helps, but SQL
Server appears to have some secret sauce!

can you send a result of EXPLAIN ANALYZE?

                                                                         QUERY PLAN                                                                        
------------------------------------------------------------------------------------------------------------------------------------------------------------
 Nested Loop Left Join  (cost=5.66..5.74 rows=1 width=132) (actual time=0.213..0.248 rows=5 loops=1)
   Join Filter: (a_1.source_code_id = a.source_code_id)
   Rows Removed by Join Filter: 4
   ->  GroupAggregate  (cost=3.65..3.67 rows=1 width=36) (actual time=0.144..0.157 rows=5 loops=1)
         Group Key: a.source_code_id
         ->  Sort  (cost=3.65..3.65 rows=1 width=10) (actual time=0.131..0.135 rows=29 loops=1)
               Sort Key: a.source_code_id
               Sort Method: quicksort  Memory: 26kB
               ->  Subquery Scan on a  (cost=2.36..3.64 rows=1 width=10) (actual time=0.063..0.116 rows=29 loops=1)
                     Filter: (a.row_num = 1)
                     Rows Removed by Filter: 3
                     ->  WindowAgg  (cost=2.36..3.24 rows=32 width=34) (actual time=0.062..0.107 rows=32 loops=1)
                           ->  Sort  (cost=2.36..2.44 rows=32 width=26) (actual time=0.054..0.059 rows=32 loops=1)
                                 Sort Key: ar_totals.location_row_id, ar_totals.function_row_id, ar_totals.source_code_id, ar_totals.tran_date DESC
                                 Sort Method: quicksort  Memory: 27kB
                                 ->  Seq Scan on ar_totals  (cost=0.00..1.56 rows=32 width=26) (actual time=0.014..0.028 rows=32 loops=1)
                                       Filter: ((tran_date <= '2018-03-31'::date) AND (deleted_id = 0) AND (ledger_row_id = 1))
   ->  GroupAggregate  (cost=2.01..2.03 rows=1 width=36) (actual time=0.017..0.017 rows=1 loops=5)
         Group Key: a_1.source_code_id
         ->  Sort  (cost=2.01..2.02 rows=1 width=10) (actual time=0.012..0.013 rows=8 loops=5)
               Sort Key: a_1.source_code_id
               Sort Method: quicksort  Memory: 25kB
               ->  Subquery Scan on a_1  (cost=1.68..2.00 rows=1 width=10) (actual time=0.032..0.047 rows=8 loops=1)
                     Filter: (a_1.row_num = 1)
                     ->  WindowAgg  (cost=1.68..1.90 rows=8 width=34) (actual time=0.031..0.043 rows=8 loops=1)
                           ->  Sort  (cost=1.68..1.70 rows=8 width=26) (actual time=0.023..0.024 rows=8 loops=1)
                                 Sort Key: ar_totals_1.location_row_id, ar_totals_1.function_row_id, ar_totals_1.source_code_id, ar_totals_1.tran_date DESC
                                 Sort Method: quicksort  Memory: 25kB
                                 ->  Seq Scan on ar_totals ar_totals_1  (cost=0.00..1.56 rows=8 width=26) (actual time=0.006..0.013 rows=8 loops=1)
                                       Filter: ((tran_date < '2018-03-01'::date) AND (deleted_id = 0) AND (ledger_row_id = 1))
                                       Rows Removed by Filter: 24
 Planning Time: 0.479 ms
 Execution Time: 0.344 ms
(33 rows)



--------------79D7BFB728F36F0C6BB1EDA4--