X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 8F8563A4614 for ; Wed, 27 Oct 2004 12:03:25 +0100 (BST) 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 95698-05 for ; Wed, 27 Oct 2004 11:03:17 +0000 (GMT) Received: from relay01.mail-hub.kbs.net.au (147.32.220.203.comindico.com.au [203.220.32.147]) by svr1.postgresql.org (Postfix) with ESMTP id 151B13A45D3 for ; Wed, 27 Oct 2004 12:03:19 +0100 (BST) Received: from [220.240.122.29] (helo=boblaptop) by relay01.mail-hub.kbs.net.au with smtp (Exim 3.36 #2) id 1CMlaG-0001nP-00 for pgsql-sql@postgresql.org; Wed, 27 Oct 2004 21:03:16 +1000 Message-ID: <001e01c4bc14$78cbda90$1d7af0dc@boblaptop> From: "Scott Pederick" To: Subject: JOIN not being calculated correctly Date: Wed, 27 Oct 2004 21:02:35 +1000 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1437 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/212 X-Sequence-Number: 19315 Hi all! I'm having a problem with Postgresql 8.0.0-beta4 on a Win2K machine and a particular join. I've got two tables - a list of customers and jobs they've had. A customer can have multiple jobs. The query always scans the entire jobs table for each customer - I need it the other way around so I can get a list of the customers who have at least one job. The EXPLAIN shows the jobs table is being scanned for some reason: Quarry=# EXPLAIN SELECT Customers.CustomerId, Jobs.JobId FROM Customers INNER JOIN Jobs USING (CustomerId); QUERY PLAN ------------------------------------------------------------------------- Hash Join (cost=78.54..4908.71 rows=70727 width=8) Hash Cond: ("outer".customerid = "inner".customerid) -> Seq Scan on jobs (cost=0.00..3769.27 rows=70727 width=8) -> Hash (cost=76.03..76.03 rows=1003 width=4) -> Seq Scan on customers (cost=0.00..76.03 rows=1003 width=4) (5 rows) Even if I reverse the JOIN I get the exact same result: Quarry=# EXPLAIN SELECT Customers.CustomerId, Jobs.JobId FROM Jobs INNER JOIN Customers USING (CustomerId); QUERY PLAN ------------------------------------------------------------------------- Hash Join (cost=78.54..4908.71 rows=70727 width=8) Hash Cond: ("outer".customerid = "inner".customerid) -> Seq Scan on jobs (cost=0.00..3769.27 rows=70727 width=8) -> Hash (cost=76.03..76.03 rows=1003 width=4) -> Seq Scan on customers (cost=0.00..76.03 rows=1003 width=4) (5 rows) How can I force it to operate as I need it to? It seems the query engine is a little smarter than it needs to be. If anyone can shed some light on this problem, it would be greatly appreciated. I've taken it as far as I can and don't really know where to move from here. Thanks in advance, Scott Pederick