From: Tambet Matiisen <t.matiisen@aprote.ee>
To: ch@rodos.fzk.de
Cc: pgsql-sql@postgresql.org
Subject: Re: How to force subquery scan?
Date: Wed, 16 Mar 2005 16:52:50 +0200
Message-ID: <A66A11DBF5525341AEF6B8DE39CDE770088058@black.aprote.com> (raw)
...
>
> Does this do better:
>
> SELECT
> prodid,
> avg(prodcost) as average_cost,
> sum(prodcost * salesamount) as cost_total
> FROM (
> SELECT
> p.product_id as prodid,
> product_cost(s.product_id, s.date) as prodcost,
> s.amount as salesamount
> FROM products p
> LEFT JOIN sales s ON s.date between '2004-01-01' and '2005-01-01'
> ) q
> GROUP BY prodid;
>
Sorry, couldn't test it as my query wasn't actual query. I just made up
something for the list. But I made some tests with similar queries and
your solution doesn't help. Subquery scan is not forced, as your query
can be easily translated into single flat query returning the same
results.
It seems that subquery scan is only used, when the query can not be
translated into single flat query. Which is mostly good, I assume. The
problem is, that if SELECT list of subquery contains function call, and
the result of this function is used in multiple places in outer query,
then the function is invoked multiple times (per row). This can get
expensive with slow function and big queries.
Tambet
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-sql@postgresql.org
Cc: t.matiisen@aprote.ee, ch@rodos.fzk.de
Subject: Re: How to force subquery scan?
In-Reply-To: <A66A11DBF5525341AEF6B8DE39CDE770088058@black.aprote.com>
References: <A66A11DBF5525341AEF6B8DE39CDE770088058@black.aprote.com>
* 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