X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 8DE079DC814 for ; Sun, 19 Feb 2006 14:15:12 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 34287-05 for ; Sun, 19 Feb 2006 14:15:11 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 2455B9DC842 for ; Sun, 19 Feb 2006 14:15:08 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id k1JIF75W011960; Sun, 19 Feb 2006 13:15:07 -0500 (EST) To: emil@baymountain.com cc: pgsql-performance@postgresql.org Subject: Re: Question about query planner In-reply-to: <200602190858.13081.emil@baymountain.com> References: <200602190858.13081.emil@baymountain.com> Comments: In-reply-to Emil Briggs message dated "Sun, 19 Feb 2006 08:58:12 -0500" Date: Sun, 19 Feb 2006 13:15:07 -0500 Message-ID: <11959.1140372907@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.11 required=5 tests=[AWL=0.110] X-Spam-Score: 0.11 X-Spam-Level: X-Archive-Number: 200602/313 X-Sequence-Number: 17299 Emil Briggs writes: > Does any know why the query behaves like this? Does it have anything to > do with the OR statements in the where clause spanning two different tables? Exactly. > SELECT DISTINCT a.account_id, l.username, a.status, a.company, a.fax_num, > a.primary_phone, a.responsible_first, a.responsible_last FROM > accounts a, logins l, supplemental_info i > WHERE l.account_id=a.account_id and > i.account_id=a.account_id and > ((a.primary_phone = 'xxx-xxx-xxxx') OR (a.alternate_phone = 'xxx-xxx-xxxx') > OR (i.contact_num = 'xxx-xxx-xxxx')) > ORDER BY a.status, a.primary_phone, a.account_id; The system has to fetch all the rows of a, because any of them might join to a row of i matching the i.contact_num condition, and conversely it has to fetch every row of i because any of them might join to a row of a matching one of the phone conditions. It is therefore necessary to effectively form the entire join of a and i; until you've done that there is no way to eliminate any rows. I'm a bit surprised that it's using the indexes at all --- a hash join with seqscan inputs would probably run faster. Try increasing work_mem a bit. regards, tom lane