Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqDVe-0008M2-Rm for pgsql-general@arkaria.postgresql.org; Wed, 24 Jul 2019 09:24:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hqDVd-0004oA-0I for pgsql-general@arkaria.postgresql.org; Wed, 24 Jul 2019 09:24:29 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqDVc-0004o3-Kq for pgsql-general@lists.postgresql.org; Wed, 24 Jul 2019 09:24:28 +0000 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqDVZ-00082n-Qe for pgsql-general@lists.postgresql.org; Wed, 24 Jul 2019 09:24:28 +0000 Received: by mail-ed1-x541.google.com with SMTP id m10so46479097edv.6 for ; Wed, 24 Jul 2019 02:24:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=VwiglkGQ8CFQPQXrBuZJJfBp2PKN76qR91mHYX1Ixl8=; b=YXjc9VlDPiGL2B7DItbiKQeVW/nQ25rAVWkbLY5Y8wqFBGjw8Z8QICeo/b21Kyqsjb zXH43griz0ExqRFN9tef90wGQbKV4lXsRv0brM2jFf9dXfYjunqfnnL9gPj9yy7lkizc IGOMv4NGrtn8lTbxofXC08+wbNgy2PVlvafslSuQDuNZUi65StlF0HpzEf6gI0KjT/aN lrLfKHRGNRkHXMolYNRO1lsv+HgQx9b1aJTAcUstWDi0SkU5K02Aq9Q6DYXBZA/3vLSa /b7+ZT0O8/nmjdx1HY1zvrSUj64jNav2zzImnIpTr15k1scIrp6UjlPMEQzOvxI7MBl+ YIyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=VwiglkGQ8CFQPQXrBuZJJfBp2PKN76qR91mHYX1Ixl8=; b=fx5St8qyfd02URDh1345sHBr0tgWfZ2l4RTdVEvg6Pms1yT+YUnMugg2KsxBAFwvRh fYhgd0LShT9ABRawHd4qsO7ebHlUTlyTkr9xbHBHeoN4+nNI3mYhCCzOR9XwuQ+Goq2+ VeIE8MEx8cOUxHn1o6dGy9I4Z7mc8mhkZzCO1J2czgZC//xdQatbWRf+F04LjywMnHsO O3doLylrqwVsm/7+3pSeB3TcBPJygCsn4R2nHKfRINjS0Lwi0ACXcqW2SnTyQe5S4jk3 xYIaKugRL9hER0hSrRGmkr7Ilp2CLJaEHjoUG7qNYWro9eXf2JmzxsQaYMMEqj43xPE9 HsZg== X-Gm-Message-State: APjAAAVUSYqgxgNt1KC9XnFQmE1G5XfmpkPDSJk6xZ77SIkLVM5H6Kxv ONyD0SbfiFPedT+076JcxOx6YSRa X-Google-Smtp-Source: APXvYqzkZtDF3GqvK5qEN9469PZ/bcpt7PEv3TcflZtfEeo0wrirUWLwPsvGHn6fMaUXu1C+QkESew== X-Received: by 2002:a50:9ec3:: with SMTP id a61mr69233929edf.184.1563960265264; Wed, 24 Jul 2019 02:24:25 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id v12sm9160183ejj.52.2019.07.24.02.24.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jul 2019 02:24:24 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Query plan: SELECT vs INSERT from same select From: Alban Hertroys In-Reply-To: Date: Wed, 24 Jul 2019 11:24:23 +0200 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <346DB5C5-3B26-4FA6-9B17-93FDED88A738@gmail.com> References: To: Alexander Voytsekhovskyy X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 23 Jul 2019, at 22:29, Alexander Voytsekhovskyy = wrote: >=20 > I have quite complicated query: >=20 > SELECT axis_x1, axis_y1, SUM(delivery_price) as v_1 FROM ( > SELECT to_char(delivery_data.delivery_date, 'YYYY-MM') as axis_x1, = clients.id_client as axis_y1, delivery_data.amount * = production_price.price * groups.discount as delivery_price >=20 > FROM delivery_data=20 > JOIN client_tt ON (client_tt.id_client_tt =3D = delivery_data.id_client_tt)=20 > JOIN clients ON (client_tt.id_client =3D clients.id_client)=20 > JOIN production ON (production.id =3D delivery_data.id_product)=20 > JOIN groups ON (groups.id =3D delivery_data.delivery_group_id AND = client_tt.id_group =3D groups.id AND groups.id =3D clients.id_group)=20 Are client_tt.id_group and clients.id_group ever different from each = other? It looks like you might have redundant information there, but... = If they are guaranteed to be the same then you don=E2=80=99t need the = JOIN to clients, which would both remove a JOIN and reduce the = complexity of the JOIN condition on groups. Or (assuming the group id=E2=80=99s are indeed supposed to be equal), = you could JOIN clients ON (client_tt.id_client =3D clients.id_client AND = client_tt.id_group =3D clients.id_group) instead of putting that condition within the JOIN condition on groups. I don=E2=80=99t think either option will make a huge difference (the = first probably more than the second, as it reduces an entire join), but = it could be enough to help the database figure out a better plan. > LEFT JOIN production_price on (delivery_data.id_product =3D = production_price.id_production AND groups.price_list_id =3D = production_price.price_list_id AND delivery_data.delivery_date BETWEEN = production_price.date_from AND production_price.date_to)=20 >=20 > WHERE delivery_data.delivery_date between '2019-03-01' AND = '2019-06-30'=20 > AND delivery_data.delivery_group_id IN (...short list of values...)=20 > AND delivery_data.id_product IN ()) AS tmpsource=20 You don=E2=80=99t have a price if your goods weren=E2=80=99t produced in = the delivery window you set? Or do you have goods that get delivered = without having a price? You seem to be using this query for a report on nett sales by month, but = I have my doubts whether that LEFT JOIN, and especially the condition on = the production date window, is really what you want: Your formula for = delivery_price includes the price column from that LEFT JOIN, so = you=E2=80=99re going to get 0 values when there is no production_price = record in your delivery-window, resulting in a SUM that=E2=80=99s too = low if the product was produced before (or after, but that seems = unlikely) the delivery window. > WHERE TRUE=20 This line is unnecessary. > GROUP BY GROUPING SETS ((axis_x1, axis_y1), (axis_x1), (axis_y1), ()) Apparently (I=E2=80=99m new to these statements), CUBE (axis_x1, = axis_y1) is a shorthand for the above. They seem to have been introduced = at the same time (in 9.6?). See: = https://www.postgresql.org/docs/11/queries-table-expressions.html#QUERIES-= GROUPING-SETS > It runs well, took 1s and returns 4000 rows. I won=E2=80=99t go into the performance issue ash this point, other more = knowledgeable people already did. Regards, Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.