Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 650B09FBBE0 for ; Tue, 31 Jul 2007 15:03:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 51934-07-2 for ; Tue, 31 Jul 2007 15:03:10 -0300 (ADT) X-Greylist: delayed 00:32:16.025761 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 49C269FBBE6 for ; Tue, 31 Jul 2007 15:03:08 -0300 (ADT) Received: from localhost ([127.0.0.1]) by ubuntu with esmtp (Exim 4.63) (envelope-from ) id 1IFvYZ-0002na-Mm for pgsql-sql@postgresql.org; Tue, 31 Jul 2007 17:30:51 +0000 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Alternative to INTERSECT User-Agent: KMail/1.9.6 MIME-Version: 1.0 Content-Disposition: inline Date: Tue, 31 Jul 2007 17:30:51 +0000 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200707311730.51280.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.051 tagged_above=0 required=5 tests=AWL=-0.050, BAYES_50=0.001, RDNS_DYNAMIC=0.1 X-Spam-Level: X-Archive-Number: 200707/198 X-Sequence-Number: 28933 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%'" on=> 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%'; id ---- 1 (1 row) Is there a way to make this more efficient with another construct, or INTERSECT the only way to accomplish the desired result? -- Andreas Joseph Krogh