Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 4320D650DBC for ; Mon, 30 Jun 2008 09:52:16 -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 13470-08 for ; Mon, 30 Jun 2008 09:52:08 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from ppsw-9.csi.cam.ac.uk (ppsw-9.csi.cam.ac.uk [131.111.8.139]) by postgresql.org (Postfix) with ESMTP id 3B36A650CF5 for ; Mon, 30 Jun 2008 09:52:13 -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]:59477 helo=flymine.flymine.org) by ppsw-9.csi.cam.ac.uk (ppsw.cam.ac.uk [131.111.8.139]:25) with esmtp id 1KDIrZ-0004mP-T7 (Exim 4.67) for pgsql-performance@postgresql.org (return-path ); Mon, 30 Jun 2008 13:52:09 +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 1KDIrY-0000bW-00 for pgsql-performance@postgresql.org; Mon, 30 Jun 2008 13:52:08 +0100 Received: from mnw21 (helo=localhost) by aragorn.flymine.org with local-esmtp (Exim 4.69) (envelope-from ) id 1KDIrY-00085o-VQ for pgsql-performance@postgresql.org; Mon, 30 Jun 2008 13:52:08 +0100 Date: Mon, 30 Jun 2008 13:52:08 +0100 (BST) From: Matthew Wakeling X-X-Sender: mnw21@aragorn.flymine.org To: pgsql-performance@postgresql.org Subject: Re: Planner should use index on a LIKE 'foo%' query In-Reply-To: <9EE32DF9-562C-45EF-829D-A877CBEC3C37@houseofdesign.de> Message-ID: References: <287E4428-BECC-46D6-BC47-EA94EA0121F0@houseofdesign.de> <20080628191931.GB13013@uio.no> <9EE32DF9-562C-45EF-829D-A877CBEC3C37@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/231 X-Sequence-Number: 30581 On Mon, 30 Jun 2008, Moritz Onken wrote: >> SELECT COUNT(*) FROM (SELECT DISTINCT result.url FROM result, item WHERE >> item.shorturl = result.url) AS a > > I tried the this approach but it's slower than WHERE IN in my case. However there's a lot more scope for improving a query along these lines, like adding indexes, or CLUSTERing on an index. It depends what other queries you are wanting to run. I don't know how much update/insert activity there will be on your database. However, if you were to add an index on the URL on both tables, then CLUSTER both tables on those indexes, and ANALYSE, then this query should run as a merge join, and be pretty quick. However, this is always going to be a long-running query, because it accesses at least one whole table scan of a large table. Matthew -- "Finger to spiritual emptiness underlying everything." -- How a foreign C manual referred to a "pointer to void."