pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: SELECT is faster on SQL Server
Date: Fri, 19 Mar 2021 10:04:10 +0100
Message-ID: <fdacd364-7203-b268-76d5-d763c0758964@gmx.net> (raw)
In-Reply-To: <fbdd2bdd-1957-1b49-eeca-0278d43de71d@chagford.com>
References: <d002a9e4-433c-3f44-90aa-46568db4bc00@chagford.com>
	<33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net>
	<fbdd2bdd-1957-1b49-eeca-0278d43de71d@chagford.com>

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;





view thread (18+ messages)  latest in thread

Message-ID: <fdacd364-7203-b268-76d5-d763c0758964@gmx.net>
Permalink:  ../fdacd364-7203-b268-76d5-d763c0758964@gmx.net/
Also on:    postgresql.org/message-id/fdacd364-7203-b268-76d5-d763c0758964@gmx.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: shammat@gmx.net
  Subject: Re: SELECT is faster on SQL Server
  In-Reply-To: <fdacd364-7203-b268-76d5-d763c0758964@gmx.net>
  References: <d002a9e4-433c-3f44-90aa-46568db4bc00@chagford.com> <33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net> <fbdd2bdd-1957-1b49-eeca-0278d43de71d@chagford.com> <fdacd364-7203-b268-76d5-d763c0758964@gmx.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox