pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: drum.lucas@gmail.com
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Optimize Query
Date: Mon, 15 Feb 2016 00:43:21 +0100
Message-ID: <95C0A385-E8A0-41B1-9DF4-4A1F039462F3@gmail.com> (raw)
In-Reply-To: <CAE_gQfXD+=LHQkAncxvh1Xo4Jvg01DspCZxmyXwNn=X-3+jrow@mail.gmail.com>
References: <CAE_gQfUASrBPpH6j8+fJNgKDUa_5EnGZEk8U9pXXGFemJg5xnQ@mail.gmail.com>
	<CANu8Fixp-oAaJpUAO8cZ-okbXQCw=izwBbygiJmzaTWUCwQpRw@mail.gmail.com>
	<CAE_gQfWEbF6jdEDNeh_8ss5y6Zg+L4zSWfoZt=6Rqmwyt17Oxg@mail.gmail.com>
	<CANu8FixNSp0OVd0M5=af9FQO94cZ0qCzqL3UK4uu3rV_7vuq5A@mail.gmail.com>
	<CAE_gQfVd8AFqBfpwkaHzDU++bvURB2ZAfftL2WwJW3siT4oyrA@mail.gmail.com>
	<CAE_gQfUZgCgmiCEKB7p5-pAfVHD7WDUJyPie4h2A6xEezbD7YQ@mail.gmail.com>
	<B072E72B-0546-415D-8944-ABCD8B052D6C@gmail.com>
	<CAE_gQfXD+=LHQkAncxvh1Xo4Jvg01DspCZxmyXwNn=X-3+jrow@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>


> On 14 Feb 2016, at 20:40, drum.lucas@gmail.com wrote:
> 
> Hi Alban! Sorry.. that was my mistake

Okay, first advice on that query: Trim it down to something that people can wrap their minds around.

You have a silly amount of code repetition in there, much of which doesn't even seem to serve a purpose. This is some kind of generated query, I gather?
For example, you don't use most of the fields from your first subquery.

Another example is the nested subquery in your left join, which can be reduced to a single subquery with just the fields summed that you actually need (and the customer_id, obviously).

The same goes for most of the joins inside that left join, definitely the left joins - but that depends a bit on your table definitions and contents.
For example, the fields you're summing come from account (but you can use customer instead, since you only use the account_id, which equals client_id anyway) and bill_item. Some fields in your where-clause come from job, some others I can't tell where they're from.

Trim, trim, trim, until you're left with a more readable query that gives you the same results and then put it through explain analyze again. It wouldn't surprise me if that query is already significantly faster.

If you're still having problems at that point, post that query and the analysis again.

> Explain analyze link:
> http://explain.depesz.com/s/5WJy

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



view thread (14+ messages)  latest in thread

Message-ID: <95C0A385-E8A0-41B1-9DF4-4A1F039462F3@gmail.com>
Permalink:  ../95C0A385-E8A0-41B1-9DF4-4A1F039462F3@gmail.com/
Also on:    postgresql.org/message-id/95C0A385-E8A0-41B1-9DF4-4A1F039462F3@gmail.com

 · 

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: haramrae@gmail.com, drum.lucas@gmail.com
  Subject: Re: Optimize Query
  In-Reply-To: <95C0A385-E8A0-41B1-9DF4-4A1F039462F3@gmail.com>
  References: <CAE_gQfUASrBPpH6j8+fJNgKDUa_5EnGZEk8U9pXXGFemJg5xnQ@mail.gmail.com> <CANu8Fixp-oAaJpUAO8cZ-okbXQCw=izwBbygiJmzaTWUCwQpRw@mail.gmail.com> <CAE_gQfWEbF6jdEDNeh_8ss5y6Zg+L4zSWfoZt=6Rqmwyt17Oxg@mail.gmail.com> <CANu8FixNSp0OVd0M5=af9FQO94cZ0qCzqL3UK4uu3rV_7vuq5A@mail.gmail.com> <CAE_gQfVd8AFqBfpwkaHzDU++bvURB2ZAfftL2WwJW3siT4oyrA@mail.gmail.com> <CAE_gQfUZgCgmiCEKB7p5-pAfVHD7WDUJyPie4h2A6xEezbD7YQ@mail.gmail.com> <B072E72B-0546-415D-8944-ABCD8B052D6C@gmail.com> <CAE_gQfXD+=LHQkAncxvh1Xo4Jvg01DspCZxmyXwNn=X-3+jrow@mail.gmail.com> <95C0A385-E8A0-41B1-9DF4-4A1F039462F3@gmail.com>

* 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