Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fK4FA-0005ER-J6 for pgsql-performance@arkaria.postgresql.org; Sat, 19 May 2018 15:58:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fK4F9-0005zI-An for pgsql-performance@arkaria.postgresql.org; Sat, 19 May 2018 15:58:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fK4F9-0005z8-0R for pgsql-performance@lists.postgresql.org; Sat, 19 May 2018 15:58:03 +0000 Received: from mail-it0-x231.google.com ([2607:f8b0:4001:c0b::231]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fK4F1-000394-5q for pgsql-performance@postgresql.org; Sat, 19 May 2018 15:58:02 +0000 Received: by mail-it0-x231.google.com with SMTP id 144-v6so16634828iti.5 for ; Sat, 19 May 2018 08:57:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=CYcxsthSWrfxuBokK6HNYAkI8LftkQSLaVpCl7Gxgx0=; b=n0T/noaTnMLl3gQE1nzc1adlv0BEYyN0gumEMouWLHTWVkzxo8r7euO2sQhbD5xZ59 rpRk1JiA1vuCrNOza6/ewyC1aL6awIcd1ghF0bH7i6O0uo7ZA0qjVp2dAWZwQPrD718G raghQ4gL7iUfK/SBSgmlUnPaQ2j9MEDDZJuIAZ3vEyxZhC7C/Xad9DU8xEt3XxidX0qm AzGIM2AK5qAB2VOyn0BnqfrB0FjFlw3CjGKrsbY+qN0zLhBUr2Sv9Q8BQrcpyCwqUHTf i7h9JdcLh3Obbhj5zFz+zuj5wzhsQU013TAVe+J2AnzY8F+IQyXoCSuMiyAkZY+25gEg X1+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=CYcxsthSWrfxuBokK6HNYAkI8LftkQSLaVpCl7Gxgx0=; b=PMjj8ly8SCTJd182TC2TxKA5oniXKvihsFys3HfpOsk5pMLl0mv/itm5NlXyDtfdXU EjqDKA3xOEEgAM8jNa0N2rPg4fovpdxhzFqyz72ysGjlPamt8yj/FzafswRklHJFRBzy 3LqLTKVtIeLpra3ON2Fy6d5gRc7KEG0yekTfqWrPiqKWoQAsNI3tGzLQ8dH/q+el7KIK Q/yfKK07BFZjW6BxktNbozahTvIamGPNiVfH9mEIUNufqkM8VnNff9cPjd2iKqhudckM ypCGICOB6zcqLQtKoVNGAR5Erq0AC87FB1KnKLtbymik/SxWwDAU4mLQqENXFmGvZazB mCPg== X-Gm-Message-State: ALKqPwcI+w/96G1TgEsfUTh7hkm3wroAkt6b/FMNFi/GUweD2VXRBrhp gevdfNhiZx3JboVLlHVlCZpVNA== X-Google-Smtp-Source: AB8JxZr+hLliCIwOSR4o4DtYeXFmU2438znCEzaNSp5enj0OM/j0GFVcCnWxIJdlMTuwL/XcMWCYvw== X-Received: by 2002:a24:5d8b:: with SMTP id w133-v6mr11226466ita.83.1526745471753; Sat, 19 May 2018 08:57:51 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id n69-v6sm6048824ioi.35.2018.05.19.08.57.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 May 2018 08:57:50 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id E8A73801727; Sat, 19 May 2018 10:57:49 -0500 (CDT) Date: Sat, 19 May 2018 10:57:49 -0500 From: Justin Pryzby To: "Kotapati, Anil" Cc: "pgsql-performance@postgresql.org" , "Kossi, Edem" , "Yadav, Rahul" , "Mukherjee, Pallavi (Contractor)" Subject: Re: Help with tuning slow query Message-ID: <20180519155749.GB30060@telsasoft.com> References: <0AA18957-DDD4-413C-AE3A-2D3FD5EF0028@comcast.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <0AA18957-DDD4-413C-AE3A-2D3FD5EF0028@comcast.com> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, May 18, 2018 at 08:32:55PM +0000, Kotapati, Anil wrote: > We are facing issues with one of our query, when we use order by count it is taking lot of time to execute the query. To be precise it is taking 9 min to execute the query from table which has ~220 million records. Is there a way to make this query run faster and efficiently using order by count. Below is the query which I’m trying to run > > Select account_number, sum(count_of_event) as "error_count" > FROM event_daily_summary > group by account_number,event_date,process_name > having event_date >= '2018-05-07' > and process_name='exp90d_xreerror' > order by sum(count_of_event) desc > limit 5000 Would you provide the information listed here ? Table definition, query plan, etc https://wiki.postgresql.org/wiki/Slow_Query_Questions Also, why "HAVING" ? Shouldn't you use WHERE ? Does the real query have conditions on event_date and process name or is that just for testing purposes? Justin