pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Joseph Krogh <andreak@officenet.no>
To: pgsql-sql@postgresql.org
Subject: Re: Help with UNION query
Date: Sun, 4 Sep 2005 20:54:00 +0200
Message-ID: <200509042054.02993.andreak@officenet.no> (raw)
In-Reply-To: <200509042008.21080.andreak@officenet.no>
References: <200509041914.33245.andreak@officenet.no>
	<200509042008.21080.andreak@officenet.no>

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@officenet.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 |                                             |
------------------------+---------------------------------------------+

view thread (4+ messages)  latest in thread

Message-ID: <200509042054.02993.andreak@officenet.no>
Permalink:  ../200509042054.02993.andreak@officenet.no/
Also on:    postgresql.org/message-id/200509042054.02993.andreak@officenet.no

 · 

reply

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: andreak@officenet.no
  Subject: Re: Help with UNION query
  In-Reply-To: <200509042054.02993.andreak@officenet.no>

* 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