X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id F0E23D8409 for ; Sun, 4 Sep 2005 15:54:06 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 07750-07 for ; Sun, 4 Sep 2005 18:54:04 +0000 (GMT) Received: from krogh.officenet.no (unknown [193.212.174.95]) by svr1.postgresql.org (Postfix) with ESMTP id 7B575D838D for ; Sun, 4 Sep 2005 15:54:02 -0300 (ADT) Received: from krogh.officenet.no (krogh.officenet.no [127.0.0.1]) by krogh.officenet.no (Postfix) with ESMTP id 296915BD23 for ; Sun, 4 Sep 2005 20:54:03 +0200 (CEST) From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Re: Help with UNION query Date: Sun, 4 Sep 2005 20:54:00 +0200 User-Agent: KMail/1.8.91 References: <200509041914.33245.andreak@officenet.no> <200509042008.21080.andreak@officenet.no> In-Reply-To: <200509042008.21080.andreak@officenet.no> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart2287381.jQA9K6LgKj"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit Message-Id: <200509042054.02993.andreak@officenet.no> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200509/29 X-Sequence-Number: 22576 --nextPart2287381.jQA9K6LgKj Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 =3D ug.groupname AND ug.username =3D u.username UNION SELECT gc.child_id FROM onp_group_children gc, onp_group g, onp_user_group ug WHERE gc.group_id =3D g.id AND g.groupname =3D ug.groupname AND ug.username= =3D=20 u.username ) AND u.username =3D '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 =3D ug.groupname AND ug.username =3D 'andreak' UNION SELECT gc.child_id =46ROM onp_group_children gc, onp_group g, onp_user_group ug WHERE gc.group_id =3D g.id AND g.groupname =3D ug.groupname AND ug.username= =3D=20 'andreak' ); So, the question stands: any idea on how to rewrite the lower wuery to only= =20 specify "ug.username=3D'andreak'" once? =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager gpg public_key: http://dev.officenet.no/~andreak/public_key.asc =2D-----------------------+---------------------------------------------+ 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=F8yen | 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 | | =2D-----------------------+---------------------------------------------+ --nextPart2287381.jQA9K6LgKj Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQBDG0LKUopImDh2gfQRAnnGAKCrJm84s8KvrIwkS4F58LfCAe6bswCggZQT F14Hfg5Kz8XFGMMygyZPi1c= =4aMW -----END PGP SIGNATURE----- --nextPart2287381.jQA9K6LgKj--