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 1gLuvr-0007F7-Tk for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 18: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 1gLuvp-0004op-43 for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 18:58:01 +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 1gLuvo-0004oi-NK for pgsql-general@lists.postgresql.org; Sun, 11 Nov 2018 18:58:00 +0000 Received: from mxc2.seznam.cz ([2a02:598:2::1023]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gLuvl-00035S-8E for pgsql-general@postgresql.org; Sun, 11 Nov 2018 18:57:59 +0000 Received: from email.seznam.cz by email-smtpc11a.ng.seznam.cz (email-smtpc11a.ng.seznam.cz [10.23.11.75]) id 49bb929aba6a1f7e49291acb; Sun, 11 Nov 2018 19:57:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=email.cz; s=beta; t=1541962671; bh=b0d8z5OdZIww03qYqOBnvhCLlYlWEhzDAh9NzL7ez/M=; h=Received:Subject:To:Cc:References:From:Message-ID:Date:User-Agent: MIME-Version:In-Reply-To:Content-Type:Content-Language: Content-Transfer-Encoding; b=dODAiASi7yztC0IKUpoxKEghOJEu34SlYceKJEyaZLTYWhwmP3g2MCPBGlES9easA QppxOjgDDxKO6w7i+v4p3SMXKrFWiZiCBVpj7P4B+b0L78fGtsLY4WNNp972q9TdGi x2zftVPFv5Gd6b9U4Di2/5LMH2dwyvKi2eK9i4ts= Received: from [192.168.0.110] (ip-94-112-39-7.net.upcbroadband.cz [94.112.39.7]) by email-relay15.ng.seznam.cz (Seznam SMTPD 1.3.102) with ESMTP; Sun, 11 Nov 2018 19:57:49 +0100 (CET) Subject: Re: query patterns for multipass aggregating To: Rob Nikander , Postgres General Cc: Andrew Gierth 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> From: =?UTF-8?Q?Ond=c5=99ej_Bouda?= Message-ID: <51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz> Date: Sun, 11 Nov 2018 19:57:52 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <71DE41FA-A835-4A2B-BD6C-BDEE498D6A3E@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: cs Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Dne 11.11.2018 v 17:20 Rob Nikander napsal(a): > I have tables `items`, `colors`, and `images`. Items have many colors, and many images. > > I want a query to list items, with their colors and images. Each result row is an item, so the colors and images must be aggregated into arrays or json. What about subqueries? SELECT items.*, (SELECT array_agg(color_name) FROM colors WHERE item_id = items.id) AS color_names, (SELECT array_agg(image_file) FROM images WHERE item_id = items.id) AS image_files FROM items According to my experience, not only the code is readable (no questions about how many rows are there for each item), but it also leads to a better query plan. Dne 11.11.2018 v 19:23 Rob Nikander napsal(a): > Could the CTE’s theoretically be optimized in a future version of PG, to work like the subqueries? I like to use them to give names to intermediate results, but I’ll stay away from them for now. It is on the todo list (for quite some time already): https://blog.2ndquadrant.com/postgresql-ctes-are-optimization-fences/ Until then, using CTEs in situations as yours leads to poor query plans as Postgres will compute the whole CTE separately (i.e., all rows involved) and only then picks some of them to the inner table. Regards, Ondřej Bouda