X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 37ACF53E69 for ; Mon, 9 May 2005 14:32:07 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 57347-04 for ; Mon, 9 May 2005 17:32:01 +0000 (GMT) Received: from priv-edtnes56.telusplanet.net (outbound01.telus.net [199.185.220.220]) by svr1.postgresql.org (Postfix) with ESMTP id 2289553699 for ; Mon, 9 May 2005 14:31:59 -0300 (ADT) Received: from localhost ([199.185.220.240]) by priv-edtnes56.telusplanet.net (InterMail vM.6.01.04.04 201-2131-118-104-20050224) with ESMTP id <20050509173159.STHF22578.priv-edtnes56.telusplanet.net@localhost>; Mon, 9 May 2005 11:31:59 -0600 Received: from 209.17.183.249 ( [209.17.183.249]) as user a3a18850@192.168.200.1 by webmail.telus.net with HTTP; Mon, 9 May 2005 10:31:58 -0700 Message-ID: <1115659918.427f9e8ee7789@webmail.telus.net> Date: Mon, 9 May 2005 10:31:58 -0700 From: Mischa Sandberg To: Russell Smith Cc: Dan Harris , pgsql-performance@postgresql.org Subject: Re: Query tuning help References: <924ac6c029f5453c442eacdda06b132c@drivefaster.net> <200505091051.14689.mr-russ@pws.com.au> <6f4a543a2fbd348c7e82876b8e477600@drivefaster.net> <200505091232.05495.mr-russ@pws.com.au> In-Reply-To: <200505091232.05495.mr-russ@pws.com.au> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.1-cvs X-Originating-IP: 209.17.183.249 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.059 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_POST, FORGED_RCVD_HELO X-Spam-Level: * X-Archive-Number: 200505/115 X-Sequence-Number: 12279 Quoting Russell Smith : > On Mon, 9 May 2005 11:49 am, Dan Harris wrote: > > On May 8, 2005, at 6:51 PM, Russell Smith wrote: > [snip] > > select distinct em.incidentid, ea.recordtext as retdata, eg.long, eg.lat > > FROM em > > JOIN ea ON em.incidentid = ea.incidentid --- slight paraphrase /Mischa. > > AND em.entrydate between '2005-1-1' and '2005-5-9' > > AND ea.recordtext like '%RED%' AND ea.recordtext like '%CORVETTE%' > > Here's the situation: > > Due to the format of the systems with which I integrate ( I have no > > control over these formats ), we will get these 'recordtext' values one > > line at a time, accumulating over time. The only way I can find to > > make this work is to insert a new record for each line. The problem > > is, that when someone wants to search multiple keywords, they expect > > these words to be matched across multiple records with a given incident > > number. > > > > For a very simple example: > > > > IncidentID Date Recordtext > > -------------- ------------- > > 11111 2005-05-01 14:21 blah blah blah RED blah blah > > 2222 2005-05-01 14:23 not what we are looking for > > 11111 2005-05-02 02:05 blah CORVETTE blah blah > > select em.incidentid, ea.recordtest as retdata from em join ( -- equivalent to "where incidentid in (...)", sometimes faster. select incidentid from em join ea using (incidentid) where em.entrydate between '2005-1-1' and '2005-5-9' group by incidentid having 1 = min(case when recordtest like '%RED%' then 1 end) and 1 = min(case when recordtest like '%CORVETTE%' then 1 end) ) as X using (incidentid);