Subselect performance question

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Subselect performance question
Date: 2002-05-02 16:37:56
Message-ID: web-1391672@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Folks,

I have a view which can be designed with eithher a sub-select in the
SELECT clause or the FROM clause. The main query data set is small
(1000 records) but the table being filtered in the sub-select is large
(110,000 records).

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;

-Josh Berkus

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Manfred Koizar 2002-05-02 20:47:13 Re: Subselect performance question
Previous Message wit 2002-05-02 15:06:35 get transaction no.