Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id F035EB5D944 for ; Thu, 23 Jun 2011 13:46:08 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 03338-06 for ; Thu, 23 Jun 2011 16:46:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from newserver.jfcomputer.com (unknown [76.14.161.106]) by mail.postgresql.org (Postfix) with ESMTP id A242BB5D91F for ; Thu, 23 Jun 2011 13:46:02 -0300 (ADT) Received: from localhost (unknown [127.0.0.1]) by newserver.jfcomputer.com (Postfix) with ESMTP id 665CB1220F for ; Thu, 23 Jun 2011 16:46:00 +0000 (UTC) X-Virus-Scanned: amavisd-new at site Received: from newserver.jfcomputer.com ([127.0.0.1]) by localhost (linux-jfp8.site [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PoMBJntNLFjR for ; Thu, 23 Jun 2011 09:45:59 -0700 (PDT) Received: from linux-desktop.localnet (unknown [192.168.1.111]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johnf@jfcomputer.com) by newserver.jfcomputer.com (Postfix) with ESMTPSA id 53B1B5AC0 for ; Thu, 23 Jun 2011 09:45:59 -0700 (PDT) From: John Fabiani Organization: J.F. Computer Solutions To: pgsql-sql@postgresql.org Subject: Re: Union out performs the single statement Date: Thu, 23 Jun 2011 09:45:58 -0700 User-Agent: KMail/1.13.5 (Linux/2.6.34.8-0.2-desktop; KDE/4.4.4; i686; ; ) References: <201106230844.49303.johnf@jfcomputer.com> In-Reply-To: <201106230844.49303.johnf@jfcomputer.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201106230945.58925.johnf@jfcomputer.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.107 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793 X-Spam-Level: X-Archive-Number: 201106/91 X-Sequence-Number: 35557 On Thursday, June 23, 2011 08:44:49 am John Fabiani wrote: > Hi, > I have a SELECT statement that is using the regexp_split_to_table function > as follows: > > ... and fk_topic in (select regexp_split_to_table(eligible_topics, > ',')::int from escourse) > > Normally there are 1 to 3 values in eligible_topics as > 46,50,43. > > The problem is the performance is terrible and I need a way to improve the > performance. I have discovered that if I separate the values in the > eligible_topics field and create a "union all" the performance is great! > The difference is with regexp_split_to_table function = 4 seconds and > using the union = 151 ms > > So the Union looks like; > > ... fk_topic = 46 ... > > union all > > ... fk_topic = 50 ... > > union all > > ... fk_topic = 43 ... > > Of course the problem is creating the unions when I don't know in advance > what the number values are in the eligible_topics field. > > The complete SQL is: > select round(miles_between_lat_long(l.latitude::numeric, > l.longitude::numeric, c.latitude::numeric, c.longitude::numeric),0) as > miles,s.began, s.ended, s.pkid as sessionid,s.stop_close, l.facility, > (select count(*) from esenroll r where r.sessionid=s.pkid) as enrolled, > l.totalseats, (select count(*) from esclass cl where cl.sessionid=s.pkid > and schedule>=current_date) as classesremaining, tp.ccode from essess s > join esloc l on l.pkid = s.locationid join esclient c on c.pkid = 36757 > join agmisc tp on tp.pkid = s.topic where s.topic in (select > regexp_split_to_table(eligible_topics, ',')::int from escourse) group by > 1,2,3,4,5,6,7,8,9,10 having (select count(*) from esclass cl where > cl.sessionid=s.pkid and schedule>= current_date and schedule <= > current_date + 30) > 0 order by 10,1 > > Without me posting the schema of the database I can see how it would be > difficult to determine the best way to tackle this issue. But I'm hoping > others will see some major issue I have created within the select. > > Thanks in advance for any help, > Johnf Thanks to all that took the time to review my SQL. I in fact solved the issue by adding a join and moving the date range to the where. Thanks for the help. Johnf