Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id A7BA7650EC4 for ; Mon, 30 Jun 2008 13:22:27 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 22865-04 for ; Mon, 30 Jun 2008 13:22:17 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from ppsw-7.csi.cam.ac.uk (ppsw-7.csi.cam.ac.uk [131.111.8.137]) by postgresql.org (Postfix) with ESMTP id 4A120650EA9 for ; Mon, 30 Jun 2008 13:21:23 -0300 (ADT) X-Cam-SpamDetails: Not scanned X-Cam-AntiVirus: No virus found X-Cam-ScannerInfo: http://www.cam.ac.uk/cs/email/scanner/ Received: from flymine.sysbiol.cam.ac.uk ([131.111.60.120]:59646 helo=flymine.flymine.org) by ppsw-7.csi.cam.ac.uk (ppsw.cam.ac.uk [131.111.8.137]:25) with esmtp id 1KDM7z-0001pR-Pj (Exim 4.67) (return-path ); Mon, 30 Jun 2008 17:21:19 +0100 Received: from host103 ([192.168.128.103] helo=aragorn.flymine.org ident=Debian-exim) by flymine.flymine.org with esmtp (Exim 4.12) id 1KDM7z-00017U-00; Mon, 30 Jun 2008 17:21:19 +0100 Received: from mnw21 (helo=localhost) by aragorn.flymine.org with local-esmtp (Exim 4.69) (envelope-from ) id 1KDM7z-0000Z4-Tu; Mon, 30 Jun 2008 17:21:19 +0100 Date: Mon, 30 Jun 2008 17:21:19 +0100 (BST) From: Matthew Wakeling X-X-Sender: mnw21@aragorn.flymine.org To: Moritz Onken cc: pgsql-performance@postgresql.org Subject: Re: Planner should use index on a LIKE 'foo%' query In-Reply-To: <0E158087-4E44-4398-96E0-BFEEAAE099A7@houseofdesign.de> Message-ID: References: <287E4428-BECC-46D6-BC47-EA94EA0121F0@houseofdesign.de> <20080628191931.GB13013@uio.no> <3D5EF684-9585-4057-BB6E-C81509635A37@houseofdesign.de> <20080630145910.GA16955@uio.no> <0E158087-4E44-4398-96E0-BFEEAAE099A7@houseofdesign.de> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/242 X-Sequence-Number: 30592 On Mon, 30 Jun 2008, Moritz Onken wrote: >>> select count(1) from result where url in (select shorturl from item >>> where shorturl = result.url); >> >> I really don't see what your query tries to accomplish. Why would you want >> "url IN (... where .. = url)"? Wouldn't you want a different qualifier >> somehow? > > well, it counts the number of rows with urls which already exist in another > table. > How would you describe the query? > If the "(select shorturl from item where shorturl = result.url)" > clause is empty the row is not counted, that's what I want... The thing here is that you are effectively causing Postgres to run a sub-select for each row of the "result" table, each time generating either an empty list or a list with one or more identical URLs. This is effectively forcing a nested loop. In a way, you have two constraints where you only need one. You can safely take out the constraint in the subquery, so it is like this: SELECT COUNT(*) FROM result WHERE url IN (SELECT shorturl FROM item); This will generate equivalent results, because those rows that didn't match the constraint wouldn't have affected the IN anyway. However, it will alter the performance, because the subquery will contain more results, but it will only be run once, rather than multiple times. This is effectively forcing a hash join (kind of). Whereas if you rewrite the query as I demonstrated earlier, then you allow Postgres to make its own choice about which join algorithm will work best. Matthew -- Anyone who goes to a psychiatrist ought to have his head examined.