Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZC7E-0002TR-J6 for pgsql-performance@arkaria.postgresql.org; Wed, 10 Jan 2018 08:52:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZC7D-0005R6-SA for pgsql-performance@arkaria.postgresql.org; Wed, 10 Jan 2018 08:52:07 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eZC7D-0005Qo-DN for pgsql-performance@lists.postgresql.org; Wed, 10 Jan 2018 08:52:07 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eZC75-0005wi-Nq for pgsql-performance@postgresql.org; Wed, 10 Jan 2018 08:52:05 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id B89B64116D; Wed, 10 Jan 2018 09:51:54 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id V0ihjiw30fmv; Wed, 10 Jan 2018 09:51:49 +0100 (CET) Message-ID: <1515574307.2848.20.camel@cybertec.at> Subject: Re: Performance of a Query From: Laurenz Albe To: Scott Marlowe , "Kumar, Virendra" Cc: "pgsql-performance@postgresql.org" Date: Wed, 10 Jan 2018 09:51:47 +0100 In-Reply-To: References: <7b438042521b45f89e510c20a798d22b@USFKL11XG20CN01.mercer.com> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Scott Marlowe wrote: > On Tue, Jan 9, 2018 at 2:18 PM, Kumar, Virendra > wrote: > > Hello Gurus, > > > > I am struggling to tune a query which is doing join on top of aggregate for > > around 3 million rows. The plan and SQL is attached to the email. > > > > Below is system Details: > > > > PGSQL version – 10.1 > > > > OS – RHEL 3.10.0-693.5.2.el7.x86_64 > > > > Binary – Dowloaded from postgres.org compiled and installed. > > > > Hardware – Virtual Machine with 8vCPU and 32GB of RAM, on XFS filesystem. > > I uploaded your query plan here: https://explain.depesz.com/s/14r6 > > The most expensive part is the merge join at the end. > > Lines like this one: "Buffers: shared hit=676 read=306596, temp > read=135840 written=135972" > > Tell me that your sorts etc are spilling to disk, so the first thing > to try is upping work_mem a bit. Don't go crazy, as it can run your > machine out of memory if you do. but doubling or tripling it and > seeing the effect on the query performance is a good place to start. > > The good news is that most of your row estimates are about right, so > the query planner is doing what it can to make the query fast, but I'm > guessing if you get the work_mem high enough it will switch from a > merge join to a hash_join or something more efficient for large > numbers of rows. Looking at the plan, I'd guess that the following index could be helpful: CREATE INDEX ON ap.site_exposure(portfolio_id, peril_id, account_id); Don't know how much it would buy you, but you could avoid the sequential scan and the sort that way. Yours, Laurenz Albe