Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 966C8650EA0 for ; Mon, 30 Jun 2008 13:42:24 -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 29858-09 for ; Mon, 30 Jun 2008 13:42:16 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from houseofdesign.de (www.houseofdesign.de [85.10.203.116]) by postgresql.org (Postfix) with ESMTP id D0252650E90 for ; Mon, 30 Jun 2008 13:42:16 -0300 (ADT) Received: from vpn-cl-165-237.rz.uni-karlsruhe.de ([141.3.165.237] helo=[192.168.1.33]) by houseofdesign.de with esmtpsa (TLS-1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.63) (envelope-from ) id 1KDMQA-0002sa-NA for pgsql-performance@postgresql.org; Mon, 30 Jun 2008 18:40:12 +0200 Cc: pgsql-performance@postgresql.org Message-Id: <35332CE1-8962-40D2-99DA-16BA80087979@houseofdesign.de> From: Moritz Onken In-Reply-To: Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v924) Subject: Re: Planner should use index on a LIKE 'foo%' query Date: Mon, 30 Jun 2008 18:42:07 +0200 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> X-Mailer: Apple Mail (2.924) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.016 tagged_above=0 required=5 tests=AWL=-0.565, MISSING_HEADERS=1.581 X-Spam-Level: * X-Archive-Number: 200806/244 X-Sequence-Number: 30594 > > 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 Thank you! I learned a lot today :-) I thought the subquery will be run on every row thus I tried to make it as fast as possible by using a where clause. I didn't try your first query on the hole table so it could be faster than mine approach. greetings, moritz