Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 9183CB5DC15 for ; Thu, 23 Jun 2011 12:53:46 -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 60698-07 for ; Thu, 23 Jun 2011 15:53:40 +0000 (UTC) X-Greylist: delayed 00:08:47.503666 by SQLgrey-1.7.6 Received: from newserver.jfcomputer.com (unknown [76.14.161.106]) by mail.postgresql.org (Postfix) with ESMTP id 60169B5DBF1 for ; Thu, 23 Jun 2011 12:53:40 -0300 (ADT) Received: from localhost (unknown [127.0.0.1]) by newserver.jfcomputer.com (Postfix) with ESMTP id 815B45ED0 for ; Thu, 23 Jun 2011 15:44:50 +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 awRnD1FLb172 for ; Thu, 23 Jun 2011 08:44:49 -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 B8AFD5AC0 for ; Thu, 23 Jun 2011 08:44:49 -0700 (PDT) From: John Fabiani Organization: J.F. Computer Solutions To: pgsql-sql@postgresql.org Subject: Union out performs the single statement Date: Thu, 23 Jun 2011 08:44:49 -0700 User-Agent: KMail/1.13.5 (Linux/2.6.34.8-0.2-desktop; KDE/4.4.4; i686; ; ) MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Message-Id: <201106230844.49303.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/90 X-Sequence-Number: 35556 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