Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id A321A9FB5C0 for ; Tue, 31 Jul 2007 15:59:11 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 60738-02-3 for ; Tue, 31 Jul 2007 15:59:04 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from netbits.us (ptr-89.fastconcepts.net [209.18.107.89]) by postgresql.org (Postfix) with SMTP id 6B19C9FBB3A for ; Tue, 31 Jul 2007 15:59:04 -0300 (ADT) Received: (qmail 22072 invoked by uid 0); 31 Jul 2007 18:59:02 -0000 Received: from trutwins.homeip.net (dhcp.209160.en-tel.net [69.46.209.160]) by fastconcepts.com ([65.17.208.228]) with SMTP via SSL; 31 Jul 2007 18:59:02 -0000 Received: (qmail 2011 invoked by uid 5033); 31 Jul 2007 18:59:00 -0000 Received: from 74.41.175.122 by rachmaninoff (envelope-from , uid 0) with qmail-scanner-1.25 (clamdscan: 0.90.2/3205. spamassassin: 3.2.0. Clear:RC:1(74.41.175.122):. Processed in 0.108557 secs); 31 Jul 2007 18:59:00 -0000 X-Qmail-Scanner-Mail-From: josh@trutwins.homeip.net via rachmaninoff X-Qmail-Scanner: 1.25 (Clear:RC:1(74.41.175.122):. Processed in 0.108557 secs) Received: from static-74-41-175-122.dsl1.mnd.mn.frontiernet.net (static-74-41-175-122.dsl1.mnd.mn.frontiernet.net [74.41.175.122]) by trutwins.homeip.net ([192.168.0.3]) with ESMTP via SSL; 31 Jul 2007 18:59:00 -0000 Date: Tue, 31 Jul 2007 13:52:22 -0500 From: Josh Trutwin To: pgsql-sql@postgresql.org Subject: Re: Alternative to INTERSECT Message-ID: <20070731135222.394da3dd@sinkhole.intrcomm.net> In-Reply-To: <200707311730.51280.andreak@officenet.no> References: <200707311730.51280.andreak@officenet.no> X-Mailer: Claws Mail 2.9.2 (GTK+ 2.10.6; i586-suse-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/199 X-Sequence-Number: 28934 On Tue, 31 Jul 2007 17:30:51 +0000 Andreas Joseph Krogh wrote: > Hi all. I have the following schema: > > CREATE TABLE test ( > id integer NOT NULL, > field character varying NOT NULL, > value character varying NOT NULL > ); > > ALTER TABLE ONLY test > ADD CONSTRAINT test_id_key UNIQUE (id, field, value); > > CREATE INDEX test_like_idx ON test USING btree (id, field, value > varchar_pattern_ops); > > Using INTERSECT I want to retrieve the rows matching (pseudo-code) > "firstname LIKE ('andrea%' OR 'jose%') AND lastname LIKE 'kro%'" Why not: WHERE (t.field = lastname AND t.value LIKE 'kro%') OR (t.field = firsname AND ( t.value LIKE 'jose%' OR t.value LIKE 'andrea%') ) Not tested. If you're having performance problems is probably less like that the INTERSECT is the problem with all those LIKE's in there? Is t.value indexed? Josh