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 1lNCw2-00085p-1T for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 11:04:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNCw0-0003ma-VL for pgsql-general@arkaria.postgresql.org; Fri, 19 Mar 2021 11:04:52 +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 1lNCvz-0003mS-Rv for pgsql-general@lists.postgresql.org; Fri, 19 Mar 2021 11:04:52 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNCvq-0001Bc-LE for pgsql-general@postgresql.org; Fri, 19 Mar 2021 11:04:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=jSVclqvwfYPRLFb6BtJH2M8D0a1FeF67mNY+J07TUho=; b=qqDdEApJiDnEw6jMyjuEEY0K/S 5Z6ijlA3vATsAiiEdHptFgKNkw+1BpX/0LskAppCBnvnYWiwAhpPeSo7sCcgLpmuNI2InfFublCoS 3iVoZe9jpZfDwBWNNixnWTlAAqJ87XEHbkZg2VMlDdNT78ZsFofTz9XhYmYV0Ky+kpKs=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1lNCvm-0000iA-IT; Fri, 19 Mar 2021 12:04:38 +0100 Date: Fri, 19 Mar 2021 12:04:38 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Frank Millman Cc: Postgres General Subject: Re: SELECT is faster on SQL Server Message-ID: <20210319110438.GA21117@depesz.com> Reply-To: depesz@depesz.com References: <33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net> <5a6f07d7-1d0f-f685-6743-d4dbc1730177@chagford.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <5a6f07d7-1d0f-f685-6743-d4dbc1730177@chagford.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Mar 19, 2021 at 12:58:10PM +0200, Frank Millman wrote: > On 2021-03-19 12:00 PM, Pavel Stehule wrote: > > In this query the most slow operation is query planning. You try to do tests on almost empty tables. This has no practical sense. > You should test queries on tables with size similar to production size. > > Sorry about that. I hope this one is better. Same query, different data set. For starters, I'm not really sure it makes sense to optimize a query that runs in 3.5 miliseconds! Having said that, after putting the plan on explain.depesz.com, I got: https://explain.depesz.com/s/xZel Which shows that ~ 50% of time was spent in scan on ar_totals and sorting it. You seem to have some really weird indexed on ar_totals created (mixed of nulls ordering). Why don't you start with simple: create index q on ar_totals (ledger_row_id, tran_date) where deleted_id = 0; But, again - either you're overthinking performance of a query that can run over 200 times per second on single core, or you're testing it with different data than the one that is really a problem. Best regards, depesz