Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cy6MK-0000GV-5V for pgsql-sql@arkaria.postgresql.org; Wed, 12 Apr 2017 00:42:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cy6MJ-00079p-Og for pgsql-sql@arkaria.postgresql.org; Wed, 12 Apr 2017 00:42:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cy6LI-0004I9-9i for pgsql-sql@postgresql.org; Wed, 12 Apr 2017 00:41:04 +0000 Received: from mail-pf0-x22c.google.com ([2607:f8b0:400e:c00::22c]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cy6LD-0003Hl-DN for pgsql-sql@postgresql.org; Wed, 12 Apr 2017 00:41:03 +0000 Received: by mail-pf0-x22c.google.com with SMTP id s16so5825016pfs.0 for ; Tue, 11 Apr 2017 17:40:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding; bh=Yl+4dFqrYszpue2XoP5r9fJ9q9H2X3iGgRjaxDSRvyY=; b=VOWNHWl1m7RLV+Fej+XXLISZzPkH2v0N4pHCabMk7vh97HZoogDwF+7bvdj3PLER// b4d7hZO12P+LLKlbh7c5PR+5ERowPdzjU+TuCUdBzGkh5KGKL7+NxKV3EmMj94GVznar yDEeMmtG7Y/+ktewsy9KasZDBy0DozjE22Q/K6c6jx1dyQ/qiLFEhBzh/RQLcv43qtxR epHSwcZLt9XrduSKnW+5SbJSvw8uc3XkefOE4UGCjwB7bcD1JdmCLDTcX8ZdcKVaquFl 2oIjOfusta6+ktSw0GUIbHSC/mOZXrD/6DZp7HEDeZHk5CTQN84UYrck1Xx90gb2D9eE eKTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding; bh=Yl+4dFqrYszpue2XoP5r9fJ9q9H2X3iGgRjaxDSRvyY=; b=AIuWvKtmwdV1Ry5+yXO9Vj4I8SQ+xTLHc9/B2gTfEiCxDBZRfe7rTKA+LfzkSpJh1/ dF/004AUWUiAqzcNAMh4nOm+ojqfd6oN0R26hJvTA9PCsEyFgL/qJamXhuliJocLCZfR fyu9uDkYgmAM7OfCaBXlUHj/xQIA/KzCUDbQafO96+bK9TmeClvrSA658oHWlepQwNjp f6Rjdj4GKmNX5AJPEhQ3E6/AvHcOLScDomPIiHPBAwpKQGP41DCgehVvaA5NB4z9SCbC ZoDaccdgfykGxBooSGl1aLAChxZu7mTz6eMYxmDMkLd5n6Q5h4v8a+YSPsu/ba1HDCiX wNPA== X-Gm-Message-State: AN3rC/7cZAjt8vD3fre+1LNR02GaG9A9RuKAXXdDSw8YplpuaLijqNuotK4ZvkISH7yJww== X-Received: by 10.98.207.66 with SMTP id b63mr8066641pfg.80.1491957656788; Tue, 11 Apr 2017 17:40:56 -0700 (PDT) Received: from [155.100.214.120] ([155.100.214.120]) by smtp.gmail.com with ESMTPSA id p16sm32747208pgc.4.2017.04.11.17.40.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Apr 2017 17:40:55 -0700 (PDT) To: "pgsql-sql@postgresql.org" From: Rob Sargent Subject: CTEs and re-use Message-ID: Date: Tue, 11 Apr 2017 18:41:22 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I have a lovely little CTE/select doing exactly what I need it to do. Unfortunately I need its results in the next query. I have this in the function def below. The gripe is that the function puts the results of the CTE/select into a temp table for the follow-on query. That mean I have a name collision and have to drop the temp table. I've tried in-lining the CTE/select put the performance is horrible. ( From 10 seconds (tolerable) to over-a-minute-and-killed intolerable. The CTE is the long pole in the tent; running it standalone takes 9.9 seconds). What am I missing here in building the fence and losing the neighbours? The CTE/select gives me the minimum value for all markers involved. The second part finds the "segment" from which that lowest p-value came, per marker. Then we reduce the list to distinct segment/p-value combinations. create or replace function optimal_pvalue_set(people_name text, markers_name text, chr int) returns table (segmentid uuid, optval numeric, firstbase int) as $$ declare mkset uuid; begin select id into mkset from seg.markerset where name = markers_name and chrom = chr; create temp table optmarkers as with segset as ( select s.id , s.chrom , s.markerset_id , s.startbase , s.endbase , ((s.events_equal + s.events_greater)/(1.0 * (s.events_less + s.events_equal + s.events_greater))) as pval from seg.segment s join seg.probandset i on s.probandset_id = i.id join (select people_id, array_agg(person_id) as persons from seg.people_member group by people_id) as pa on i.probands <@ pa.persons join seg.people o on pa.people_id = o.id where s.markerset_id = mkset and o.name = people_name ) select m.id as mkrid , min(ss.pval) as optval from segset ss join seg.markerset_member mm on ss.markerset_id = mm.markerset_id join seg.marker m on mm.member_id = m.id where m.basepos between ss.startbase and ss.endbase and m.chrom = ss.chrom and mm.markerset_id = mkset group by m.id; return query select s.id, o.optval, min(m.basepos) as firstbase from optmarkers o --- <<<<----------------------------Tried in-lining the CTE here. join seg.marker m on o.mkrid = m.id join seg.markerset_member mm on m.id = mm.member_id join seg.segment s on mm.markerset_id = s.markerset_id where mm.markerset_id = mkset and m.basepos between s.startbase and s.endbase and ((s.events_equal + s.events_greater)/(1.0 * (s.events_less + s.events_equal + s.events_greater))) = o.optval group by s.id, o.optval order by firstbase; end; $$ language plpgsql; -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql