X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 6F233476C6A for ; Sat, 17 May 2003 02:51:30 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 10394-04 for ; Sat, 17 May 2003 02:51:29 -0400 (EDT) Received: from fw1.ultimeth.net (bdsl.66.13.45.202.gte.net [66.13.45.202]) by postgresql.org (Postfix) with ESMTP id 9332B476C15 for ; Sat, 17 May 2003 02:51:28 -0400 (EDT) Received: from TpA22p1.ultimeth.net (TpA22p1.dhcp.private.ultimeth.net [192.168.128.199]) (authenticated) by fw1.ultimeth.net (8.11.6/8.11.6) with ESMTP id h4H6pSA17542 for ; Fri, 16 May 2003 23:51:28 -0700 Message-Id: <5.1.0.14.2.20030516231811.02deac70@imaps.ultimeth.net> X-Sender: mailer@dba-sql.ultimeth.net (Unverified) X-Mailer: QUALCOMM Windows Eudora Version 5.1 Date: Fri, 16 May 2003 23:51:20 -0700 To: pgsql-sql@postgresql.org From: "Dean Gibson (DB Administrator)" Subject: JOIN vs. WHERE ... IN (subselect) Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/357 X-Sequence-Number: 13522 Using PostgreSQL 7.3.2 on Linux. One of the types of queries that I like to do on our database is: SELECT * FROM table1 WHERE indexed_column1 IN (SELECT column2 FROM table2 WHERE ); However, in our database table1 is quite large (~1M rows), and the above query takes "forever", EVEN IF table2 CONSISTS OF ONLY ONE ROW! However, my third-party SQL book says that the above is equivalent to: SELECT table1.* FROM table1, table2 WHERE indexed_column1 = column2 AND ; And indeed, the above query executes virtually instantaneously if "" results in a small number of rows from table2. I'm fairly new to SQL; are they really equivalent? If so, is there some reason that the first form is not optimized/transformed into the second form, or is this a planned enhancement? -- Dean ps: If indexed_column1 has the same name as column2, then the query: SELECT table1.* from table1 JOIN table2 USING( column ) WHERE ; Also executes quickly. I just like the readability of the first query above, but as they say, you gotta do what works ...