Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 055139FB3A1 for ; Wed, 1 Aug 2007 22:46:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 93736-10 for ; Wed, 1 Aug 2007 22:46:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from andromeda1.neonex.com.br (dns3.neonex.com.br [201.28.22.10]) by postgresql.org (Postfix) with ESMTP id 797579FB39B for ; Wed, 1 Aug 2007 22:46:09 -0300 (ADT) Received: from [200.206.25.4] (helo=[127.0.0.1]) by andromeda1.neonex.com.br with esmtpa (Exim 4.63) (envelope-from ) id 1IGPlM-0000Bp-S6; Wed, 01 Aug 2007 22:46:06 -0300 Message-ID: <46B13758.9000204@planit.com.br> Date: Wed, 01 Aug 2007 22:46:00 -0300 From: "Luiz K. Matsumura" Organization: Plan IT Tecnologia =?ISO-8859-1?Q?Inform=E1tica_Ltda=2E?= User-Agent: Thunderbird 2.0.0.5 (Windows/20070716) MIME-Version: 1.0 To: Andreas Joseph Krogh CC: pgsql-sql@postgresql.org References: <200707311730.51280.andreak@officenet.no> <20070731135222.394da3dd@sinkhole.intrcomm.net> <200707311945.16573.andreak@officenet.no> In-Reply-To: <200707311945.16573.andreak@officenet.no> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Antivirus: avast! (VPS 000763-1, 08/01/2007), Outbound message X-Antivirus-Status: Clean X-SA-Exim-Connect-IP: 200.206.25.4 X-SA-Exim-Rcpt-To: andreak@officenet.no, pgsql-sql@postgresql.org Subject: Re: Alternative to INTERSECT X-SA-Exim-Version: 4.2.1 (built Tue, 09 Jan 2007 17:23:22 +0000) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/11 X-Sequence-Number: 28949 I don't know if this is more efficient but an alternative can be something like this SELECT t.id FROM test t JOIN test t2 ON t2.id = t.id AND t2.field = 'firstname' AND t2.value LIKE 'jose%' JOIN test t3 ON t3.id = t2.id AND t3.field = 'lastname' AND t3.value LIKE 'kro%' WHERE t.field = 'firstname' AND t.value LIKE 'andrea%' Hope this helps Andreas Joseph Krogh wrote: > 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 = 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? >> > > Yes, as I wrote: > > CREATE INDEX test_like_idx ON test USING btree > (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 = 'firstname' AND t.value > LIKE 'andrea%' > INTERSECT SELECT t.id FROM test t WHERE t.field = 'firstname' AND t.value > LIKE 'jose%' > INTERSECT SELECT t.id FROM test t WHERE t.field = 'lastname' AND t.value > LIKE 'kro%'; > > To return only id 1, and the query: > > SELECT t.id from test t WHERE t.field = 'firstname' AND t.value > LIKE 'andrea%' > INTERSECT SELECT t.id FROM test t WHERE t.field = 'firstname' AND t.value > LIKE 'jose%' > INTERSECT SELECT t.id FROM test t WHERE t.field = 'lastname' AND t.value > LIKE 'non%'; > > To return no rows at all (cause nobydy's name is "andreas joseph noname"). > > Your suggestion doesn't cover this case. > > -- > AJK > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster > > > -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda.