From: | Andreas Joseph Krogh <andreak(at)officenet(dot)no> |
---|---|
To: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Help with UNION query |
Date: | 2005-09-04 18:54:00 |
Message-ID: | 200509042054.02993.andreak@officenet.no |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
On Sunday 04 September 2005 20:08, Andreas Joseph Krogh wrote:
> Replying to my self...
Again...
I managed to "reduse" my query to the following:
SELECT gr.groupname FROM onp_group gr, onp_user u
WHERE gr.id IN
(
SELECT g.id FROM
onp_group g, onp_user_group ug
WHERE g.groupname = ug.groupname AND ug.username = u.username
UNION
SELECT gc.child_id FROM
onp_group_children gc, onp_group g, onp_user_group ug
WHERE gc.group_id = g.id AND g.groupname = ug.groupname AND ug.username =
u.username
)
AND u.username = 'andreak';
But "EXPLAIN ANALYZE" tells me it's about 8x slower than this one:
SELECT gr.groupname FROM onp_group gr WHERE gr.id IN(
SELECT g.id
from onp_group g, onp_user_group ug
where g.groupname = ug.groupname AND ug.username = 'andreak'
UNION
SELECT gc.child_id
FROM onp_group_children gc, onp_group g, onp_user_group ug
WHERE gc.group_id = g.id AND g.groupname = ug.groupname AND ug.username =
'andreak'
);
So, the question stands: any idea on how to rewrite the lower wuery to only
specify "ug.username='andreak'" once?
--
Andreas Joseph Krogh <andreak(at)officenet(dot)no>
Senior Software Developer / Manager
gpg public_key: http://dev.officenet.no/~andreak/public_key.asc
------------------------+---------------------------------------------+
OfficeNet AS | The most difficult thing in the world is to |
Hoffsveien 17 | know how to do a thing and to watch |
PO. Box 425 Skøyen | somebody else doing it wrong, without |
0213 Oslo | comment. |
NORWAY | |
Phone : +47 22 13 01 00 | |
Direct: +47 22 13 10 03 | |
Mobile: +47 909 56 963 | |
------------------------+---------------------------------------------+
From | Date | Subject | |
---|---|---|---|
Next Message | Antony Sohal | 2005-09-05 10:20:28 | Weird Trigger Behaviour using IF ... THEN ... ESLEIF... THEN... ELSEIF... THEN ... ELSE ... END IF; |
Previous Message | Andreas Joseph Krogh | 2005-09-04 18:08:20 | Re: Help with UNION query |