From: Tom Lane <tgl@sss.pgh.pa.us>
To: Josh Berkus <josh@agliodbs.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Subselect performance question
Date: Thu, 02 May 2002 16:56:13 -0400
Message-ID: <18063.1020372973@sss.pgh.pa.us> (raw)
In-Reply-To: <web-1391672@davinci.ethosmedia.com>
References: <web-1391672@davinci.ethosmedia.com>
"Josh Berkus" <josh@agliodbs.com> writes:
> Under those circumstances, is there a hard-and-fast rule for which
> query I should use? Most of the time, users will only look at one
> record at a time from the main data set.
> SELECT client_id, client_name,
> (SELECT count(*) FROM case_clients WHERE client_id = clients.client_id
> AND case_status = 'ACT') as active_count,
> (SELECT count(*) FROM case_clients WHERE client_id = clients.client_id
> AND case_status = 'STL') as settled_count,
> FROM clients;
> OR:
> SELECT client_id, client_name, active_count, settled_count,
> FROM clients
> LEFT OUTER JOIN (SELECT client_id, count(*) as active_count FROM
> case_clients WHERE case_status = 'ACT' GROUP BY client_id) ac ON
> ac.client_id = clients.client_id
> LEFT OUTER JOIN (SELECT client_id, count(*) as settled_count FROM
> case_clients WHERE case_status = 'STL' GROUP BY client_id) sc ON
> sc.client_id = clients.client_id;
The second one looks like a sure loser to me, because of the GROUP BYs.
If you were expecting queries to retrieve many different client_ids,
it *might* be better to use the second form. But I think for a small
number of client_ids the first will be quicker.
regards, tom lane
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, josh@agliodbs.com
Subject: Re: Subselect performance question
In-Reply-To: <18063.1020372973@sss.pgh.pa.us>
* 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