pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Ondřej Bouda <obouda@email.cz>
Cc: Rob Nikander <rob.nikander@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: query patterns for multipass aggregating
Date: Sun, 11 Nov 2018 19:20:58 +0000
Message-ID: <871s7rwhfk.fsf@news-spur.riddles.org.uk> (raw)
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>

>>>>> "Ondřej" == Ondřej Bouda <obouda@email.cz> writes:

 Ondřej> What about subqueries?

 Ondřej> SELECT
 Ondřej>     items.*,
 Ondřej>     (SELECT array_agg(color_name) FROM colors WHERE item_id =
 Ondřej> items.id) AS color_names,
 Ondřej>     (SELECT array_agg(image_file) FROM images WHERE item_id =
 Ondřej> items.id) AS image_files
 Ondřej> FROM items

 Ondřej> According to my experience, not only the code is readable (no
 Ondřej> questions about how many rows are there for each item), but it
 Ondřej> 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).

-- 
Andrew (irc:RhodiumToad)




view thread (6+ messages)

Message-ID: <871s7rwhfk.fsf@news-spur.riddles.org.uk>
Permalink:  ../871s7rwhfk.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/871s7rwhfk.fsf@news-spur.riddles.org.uk

 · 

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: andrew@tao11.riddles.org.uk, obouda@email.cz, rob.nikander@gmail.com
  Subject: Re: query patterns for multipass aggregating
  In-Reply-To: <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> <871s7rwhfk.fsf@news-spur.riddles.org.uk>

* 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