pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: 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 17:41:49 +0000
Message-ID: <87d0rbwp4a.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <11335409-A083-4951-BC7A-3C4FD118AC04@gmail.com>
References: <11335409-A083-4951-BC7A-3C4FD118AC04@gmail.com>

>>>>> "Rob" == Rob Nikander <rob.nikander@gmail.com> writes:

 Rob> I want a query to list items, with their colors and images. Each
 Rob> result row is an item, so the colors and images must be aggregated
 Rob> into arrays or json.

 Rob> If there were only one other table, it’s a simple join and group…

 Rob>     select items.*, array_agg(color_name)
 Rob>     from items join colors on items.id = colors.item_id
 Rob>     group by items.id    

Method 1:

select items.*, c.colors, i.images
  from items
       left join (select item_id, array_agg(color_name) as colors
                    from colors
                   group by item_id) c
         on c.item_id=items.id
       left join (select item_id, array_agg(image) as images
                    from images
                   group by item_id) i
         on i.item_id=items.id;

Method 2:

select items.*, c.colors, i.images
  from items
       left join lateral (select array_agg(c0.color_name) as colors
                            from colors c0
                           where c0.item_id=items.id) c
         on true
       left join lateral (select array_agg(i0.image) as images
                            from images i0
                           where i0.item_id=items.id) i
         on true;

Unfortunately, the planner isn't smart enough yet to know that these two
are equivalent, so they generate different sets of possible query plans.
Method 1 gets plans that work well if the entire items table is being
selected, since it will read the whole of the images and colors tables
in one go, and it will also get plans that work well for reading a
_single_ item selected by WHERE items.id=? because equivalence-class
processing will propagate a copy of that condition down to below the
grouping clauses. It will _not_ get a good plan for reading any other
small subset of items (e.g. selected by other criteria); for this you
need method 2, which in turn doesn't get very good plans when you fetch
the whole items table.

Don't be tempted to use CTEs for the subqueries in either plan; that'll
only make it much worse.

-- 
Andrew (irc:RhodiumToad)




view thread (6+ messages)  latest in thread

Message-ID: <87d0rbwp4a.fsf@news-spur.riddles.org.uk>
Permalink:  ../87d0rbwp4a.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87d0rbwp4a.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, rob.nikander@gmail.com
  Subject: Re: query patterns for multipass aggregating
  In-Reply-To: <87d0rbwp4a.fsf@news-spur.riddles.org.uk>
  References: <11335409-A083-4951-BC7A-3C4FD118AC04@gmail.com> <87d0rbwp4a.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