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 1gLvIC-0008VX-Kl for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 19:21:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gLvIA-00023B-SI for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 19:21:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gLvIA-000234-Bs for pgsql-general@lists.postgresql.org; Sun, 11 Nov 2018 19:21:06 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gLvI6-0003bW-VU for pgsql-general@postgresql.org; Sun, 11 Nov 2018 19:21:04 +0000 Received: from [192.168.127.1] (port=30575 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gLvI3-000E3V-NP; Sun, 11 Nov 2018 19:20:59 +0000 Received: from [127.0.0.1] (port=55204 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gLvI3-000P2M-2Q; Sun, 11 Nov 2018 19:20:59 +0000 From: Andrew Gierth To: =?utf-8?Q?Ond=C5=99ej?= Bouda Cc: Rob Nikander , Postgres General Subject: Re: query patterns for multipass aggregating In-Reply-To: <51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz> (=?utf-8?Q?=22Ond=C5=99ej?= Bouda"'s message of "Sun, 11 Nov 2018 19:57:52 +0100") Message-ID: <871s7rwhfk.fsf@news-spur.riddles.org.uk> References: <11335409-A083-4951-BC7A-3C4FD118AC04@gmail.com> <87d0rbwp4a.fsf@news-spur.riddles.org.uk> <878t1zwlh8.fsf@news-spur.riddles.org.uk> <71DE41FA-A835-4A2B-BD6C-BDEE498D6A3E@gmail.com> <51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sun, 11 Nov 2018 19:20:58 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Ond=C5=99ej" =3D=3D Ond=C5=99ej Bouda writes: Ond=C5=99ej> What about subqueries? Ond=C5=99ej> SELECT Ond=C5=99ej> items.*, Ond=C5=99ej> (SELECT array_agg(color_name) FROM colors WHERE item_id = =3D Ond=C5=99ej> items.id) AS color_names, Ond=C5=99ej> (SELECT array_agg(image_file) FROM images WHERE item_id = =3D Ond=C5=99ej> items.id) AS image_files Ond=C5=99ej> FROM items Ond=C5=99ej> According to my experience, not only the code is readable (no Ond=C5=99ej> questions about how many rows are there for each item), but it Ond=C5=99ej> also leads to a better query plan. This is (generally speaking) no improvement over the LATERAL method I showed, and is less flexible (for example it's very painful to return more than one value from the subqueries). --=20 Andrew (irc:RhodiumToad)