X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9A7909FA48C for ; Sun, 28 May 2006 19:39:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 81906-09 for ; Sun, 28 May 2006 19:38:58 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 624699FA131 for ; Sun, 28 May 2006 19:38:59 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 73C8931963; Mon, 29 May 2006 00:38:57 +0200 (MET DST) Date: Mon, 29 May 2006 00:38:55 +0200 From: Erwin Brandstetter User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 X-Newsgroups: comp.databases.postgresql,pgsql.performance Subject: Re: Query performance References: <1148294589.715695@proxy.dienste.wien.at> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: X-Complaints-To: abuse@chello.at Organization: chello.at Lines: 65 To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/546 X-Sequence-Number: 19333 Antonio Batovanja wrote: (...) > 1) the slooooow query: > EXPLAIN ANALYZE SELECT DISTINCT ldap_entries.id, organization.id, > text('organization') AS objectClass, ldap_entries.dn AS dn FROM > ldap_entries, organization, ldap_entry_objclasses WHERE > organization.id=ldap_entries.keyval AND ldap_entries.oc_map_id=1 AND > upper(ldap_entries.dn) LIKE '%DC=HUMANOMED,DC=AT' AND 1=1 OR > (ldap_entries.id=ldap_entry_objclasses.entry_id AND > ldap_entry_objclasses.oc_name='organization'); First, presenting your query in any readable form might be helpful if you want the community to help you. (Hint! Hint!) SELECT DISTINCT ldap_entries.id, organization.id, text('organization') AS objectClass, ldap_entries.dn AS dn FROM ldap_entries, organization, ldap_entry_objclasses WHERE organization.id=ldap_entries.keyval AND ldap_entries.oc_map_id=1 AND upper(ldap_entries.dn) LIKE '%DC=HUMANOMED,DC=AT' AND 1=1 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='organization'); Next, you might want to use aliases to make it more readable. SELECT DISTINCT e.id, o.id, text('organization') AS objectClass, e.dn AS dn FROM ldap_entries AS e, organization AS o, ldap_entry_objclasses AS eo WHERE o.id=e.keyval AND e.oc_map_id=1 AND upper(e.dn) LIKE '%DC=HUMANOMED,DC=AT' AND 1=1 OR (e.id=eo.entry_id AND eo.oc_name='organization'); There are a couple redundant (nonsensical) items, syntax-wise. Let's strip these: SELECT DISTINCT e.id, o.id, text('organization') AS objectClass, e.dn FROM ldap_entries AS e, organization AS o, ldap_entry_objclasses AS eo WHERE o.id=e.keyval AND e.oc_map_id=1 AND e.dn ILIKE '%DC=HUMANOMED,DC=AT' OR e.id=eo.entry_id AND eo.oc_name='organization'; And finally, I suspect the lexical precedence of AND and OR might be the issue here. http://www.postgresql.org/docs/8.1/static/sql-syntax.html#SQL-PRECEDENCE Maybe that is what you really want (just guessing): SELECT DISTINCT e.id, o.id, text('organization') AS objectClass, e.dn FROM ldap_entries e JOIN organization o ON o.id=e.keyval LEFT JOIN ldap_entry_objclasses eo ON eo.entry_id=e.id WHERE e.oc_map_id=1 AND e.dn ILIKE '%DC=HUMANOMED,DC=AT' OR eo.oc_name='organization)'; I didn't take the time to read the rest. My appologies if I guessed wrong. Regards, Erwin