Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 535F19FBA97 for ; Tue, 31 Jul 2007 16:45:25 -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 96537-08 for ; Tue, 31 Jul 2007 16:45:18 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from kroghlaptop.officenet.no (79.84-48-119.nextgentel.com [84.48.119.79]) by postgresql.org (Postfix) with ESMTP id 062989FB60A for ; Tue, 31 Jul 2007 16:45:20 -0300 (ADT) Received: from localhost ([127.0.0.1]) by ubuntu with esmtp (Exim 4.63) (envelope-from ) id 1IFxeg-0003Fm-RF for pgsql-sql@postgresql.org; Tue, 31 Jul 2007 19:45:18 +0000 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Re: Alternative to INTERSECT Date: Tue, 31 Jul 2007 19:45:10 +0000 User-Agent: KMail/1.9.6 References: <200707311730.51280.andreak@officenet.no> <20070731135222.394da3dd@sinkhole.intrcomm.net> In-Reply-To: <20070731135222.394da3dd@sinkhole.intrcomm.net> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200707311945.16573.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/201 X-Sequence-Number: 28936 On Tuesday 31 July 2007 18:52:22 Josh Trutwin wrote: > 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 =3D lastname AND t.value LIKE 'kro%') > OR (t.field =3D 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? Yes, as I wrote: CREATE INDEX test_like_idx ON test USING btree=20 (id, field, value varchar_pattern_ops); And I'm observing that it uses that index. Your query doesn't cut it, let me try to explain what I'm trying to achieve: Suppose I have the following data: INSERT INTO test VALUES (1, 'firstname', 'andreas'); INSERT INTO test VALUES (1, 'firstname', 'joseph'); INSERT INTO test VALUES (1, 'lastname', 'krogh'); INSERT INTO test VALUES (2, 'firstname', 'andreas'); INSERT INTO test VALUES (2, 'lastname', 'noname'); The reason for why I use INTERSECT is that I want: SELECT t.id from test t WHERE t.field =3D 'firstname' AND t.value=20 LIKE 'andrea%' INTERSECT SELECT t.id FROM test t WHERE t.field =3D 'firstname' AND t.value= =20 LIKE 'jose%' INTERSECT SELECT t.id FROM test t WHERE t.field =3D 'lastname' =A0AND t.val= ue=20 LIKE 'kro%'; To return only id 1, and the query: SELECT t.id from test t WHERE t.field =3D 'firstname' AND t.value=20 LIKE 'andrea%' INTERSECT SELECT t.id FROM test t WHERE t.field =3D 'firstname' AND t.value= =20 LIKE 'jose%' INTERSECT SELECT t.id FROM test t WHERE t.field =3D 'lastname' =A0AND t.val= ue=20 LIKE 'non%'; To return no rows at all (cause nobydy's name is "andreas joseph noname"). Your suggestion doesn't cover this case. =2D- AJK