pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ondřej Bouda <obouda@email.cz>
To: Rob Nikander <rob.nikander@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>
Subject: Re: query patterns for multipass aggregating
Date: Sun, 11 Nov 2018 19:57:52 +0100
Message-ID: <51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz> (raw)
In-Reply-To: <71DE41FA-A835-4A2B-BD6C-BDEE498D6A3E@gmail.com>
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>

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




view thread (6+ messages)  latest in thread

Message-ID: <51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz>
Permalink:  ../51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz/
Also on:    postgresql.org/message-id/51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz

 · 

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: obouda@email.cz, rob.nikander@gmail.com, andrew@tao11.riddles.org.uk
  Subject: Re: query patterns for multipass aggregating
  In-Reply-To: <51a95440-c9c8-7240-e7eb-4b1a62ebf6b4@email.cz>
  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>

* 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