Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aV6MQ-0007uT-QU for pgsql-general@arkaria.postgresql.org; Sun, 14 Feb 2016 23:45:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aV6MP-0006vb-1S for pgsql-general@arkaria.postgresql.org; Sun, 14 Feb 2016 23:45:49 +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) (envelope-from ) id 1aV6K8-0004Ju-BK for pgsql-general@postgresql.org; Sun, 14 Feb 2016 23:43:28 +0000 Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aV6K5-0002ed-1t for pgsql-general@postgresql.org; Sun, 14 Feb 2016 23:43:26 +0000 Received: by mail-wm0-x22e.google.com with SMTP id g62so128468345wme.0 for ; Sun, 14 Feb 2016 15:43:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=wgM3EKvJg6U7AzPv3Taml+UMk5o/fWx9SjQADzvkKwk=; b=QsPfUzwNXN6cYdxM0wqFpOu39rqvF6VZcfEjtJTdZoNGpi+kRZmZKdX/mIW+Lwsl3i K0idryDLYc51sH0uES/R+1oDmbMEkoXxm2DQIgTz3/5JnEHoB0wcIxwGzoaHrWpBrhul SoEi6n+gNl7EivBHYfPAMCl6wypAdRzuI2+VoJQ9KukkltNaYC3ZU7xOFuOQV4wPj05r WMDs9LMPhfKMn5GgsfKN3re7yeisnjBQyRk34/3jWNhXh8NCMj/5RqYnF29dqWOd6du8 /AQAt5gHL2bkMmqgARAe3BeMFhewAJhwmSKuTNY8RPGceSoOu3zusY1FUcJ1QrTJrOUh x2jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:content-type:mime-version:subject:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to; bh=wgM3EKvJg6U7AzPv3Taml+UMk5o/fWx9SjQADzvkKwk=; b=eQtRiFOlMf9lqJe0xi4L7d4S0azS22l6/KPtgcaM5yFjO+iXvz7w/ZhOGICbAm1WVO 1khwcCTbPnG8Xto0ndTHJebkwsCxXMmWXV82ZePve03gx5CRziUOKTe+YIx5XIVXU/nw RCSATpnp7ll/SqiPs/PLiHoZh/clkLdSHU4XCZpd4cTNl1mXm6TJ4KHot+lwO/Spn61t 5VBeKqu3tL3gmrplFV4HAZ4SP02RnUS27cTBiFXpfAOaQJ8mJf4/hdPC+3HyGkGwOab5 AEj+QnR+guULvIfTbmFAY06iQAio/d6mCjCJYAVtjrMgShTjNvSbHY56u+4+7WC/X/H8 TpMw== X-Gm-Message-State: AG10YOQfFz/VX/C9vyv9X1FJsJS7oSBC/TDtRpFWa/SQn6tayQ259iSG/yRbH6+cDbKNcQ== X-Received: by 10.194.11.67 with SMTP id o3mr12874810wjb.74.1455493403020; Sun, 14 Feb 2016 15:43:23 -0800 (PST) Received: from hollewijn.internal (8d690a59.ftth.concepts.nl. [141.105.10.89]) by smtp.gmail.com with ESMTPSA id y3sm3273504wmy.17.2016.02.14.15.43.21 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 14 Feb 2016 15:43:22 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) Subject: Re: Optimize Query From: Alban Hertroys In-Reply-To: Date: Mon, 15 Feb 2016 00:43:21 +0100 Cc: Postgres General Content-Transfer-Encoding: quoted-printable Message-Id: <95C0A385-E8A0-41B1-9DF4-4A1F039462F3@gmail.com> References: To: drum.lucas@gmail.com X-Mailer: Apple Mail (2.3112) X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org > On 14 Feb 2016, at 20:40, drum.lucas@gmail.com wrote: >=20 > 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 gathe= r? 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 redu= ced 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 l= eft 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 c= ustomer 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 y= ou the same results and then put it through explain analyze again. It would= n't surprise me if that query is already significantly faster. If you're still having problems at that point, post that query and the anal= ysis 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. --=20 Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general