From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@postgresql.org
Subject: Using distinct in an aggregate prevents parallel execution?
Date: Wed, 6 Jun 2018 14:41:25 +0200
Message-ID: <pf8kli$i7a$1@blaine.gmane.org> (raw)
Consider this simplified example:
select c.id,
count(*) as total_orders,
sum(p.price) as total_value
from customer c
join orders o ON c.id = o.customer_id
join order_line ol ON o.id = ol.order_id
join product p ON ol.product_id = p.id
group by c.id;
This uses parallel execution quite nicely: https://explain.depesz.com/s/aSPNn
However, the query is incorrect as it does not count the number of orders, but (essentially) the number of order_lines.
This can easily be fixed using:
select c.id,
count(distinct o.id) as total_orders,
sum(p.price) as total_value
from customer c
join orders o ON c.id = o.customer_id
join order_line ol ON o.id = ol.order_id
join product p ON ol.product_id = p.id
group by c.id;
But in that case Postgres 10.4 decides to no longer use parallel execution: https://explain.depesz.com/s/7Ua3
Which increases the query execution time quite a bit (from 3 to 8 seconds).
Is this a known limitation?
Thomas
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: spam_eater@gmx.net
Subject: Re: Using distinct in an aggregate prevents parallel execution?
In-Reply-To: <pf8kli$i7a$1@blaine.gmane.org>
References: <pf8kli$i7a$1@blaine.gmane.org>
* 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