Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XrSSy-0005Ra-4m for pgsql-sql@arkaria.postgresql.org; Thu, 20 Nov 2014 14:12:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XrSSx-0004p8-2K for pgsql-sql@arkaria.postgresql.org; Thu, 20 Nov 2014 14:12:11 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XrSSv-0004p1-VT for pgsql-sql@postgresql.org; Thu, 20 Nov 2014 14:12:10 +0000 Received: from mail-wg0-x235.google.com ([2a00:1450:400c:c00::235]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XrSSr-0000Tb-Gs for pgsql-sql@postgresql.org; Thu, 20 Nov 2014 14:12:07 +0000 Received: by mail-wg0-f53.google.com with SMTP id l18so3787284wgh.40 for ; Thu, 20 Nov 2014 06:12:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=SxMsOadwrfNzNGew0GmjIWYUPmKGNn6/XZ2DUA6yv2M=; b=v0pgrZYRkfky6kEW7xt3luYywwDFM80mwR533DzyzF5mPFD0DpEtuHQUHUVPUaNFpG i9iar7j2Aq0M7TWr8/G5ePhv4OioQ4yskUNjnX4ZP0huP3k6bYSGGuMkNQLPPzfW18Uv AqkdVMrTFYluYGlsis/eZ8OZVc5WzE9bhBQjFCarb1N+85gBxCctdFiZsHxb/DCGbgVv xnXJpewB6nL4uwHTPgFIbsqiS4wJfKBjpV7UB9SP9ZtT1m4TcvKr9b+3kZBAD2MtoMrd I2JG8t+yQwK9ouhumtWnMsfU+2K7xk86kdHjt9k+SjOMng9ZQPZbGGIE0k6/w6zAvMx0 xAeQ== X-Received: by 10.180.99.163 with SMTP id er3mr4276709wib.18.1416492722864; Thu, 20 Nov 2014 06:12:02 -0800 (PST) Received: from [192.168.1.64] (host217-43-225-174.range217-43.btcentralplus.com. [217.43.225.174]) by mx.google.com with ESMTPSA id ce1sm3560569wjc.2.2014.11.20.06.12.01 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 20 Nov 2014 06:12:02 -0800 (PST) Message-ID: <546DF6B0.4040001@gmail.com> Date: Thu, 20 Nov 2014 14:12:00 +0000 From: Tim Dudgeon User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: slow sub-query problem References: <546A3FBA.9020901@gmail.com> <1416249886486-5827275.post@n5.nabble.com> <546B09F3.90609@gmail.com> <1416361021024-5827453.post@n5.nabble.com> In-Reply-To: <1416361021024-5827453.post@n5.nabble.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) 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 tried them all out. Original query: 17039ms Simple join: 889ms Join with SELECT: 1302ms (799ms without DISTINCT which I don't think is needed here) Using INTERSECT: 1454ms (1474 without DISTINCT) So with the current data the simple join and the Join with SELECT but no DISTINCT are the best. Thanks for your help with this. Tim On 19/11/2014 01:37, David G Johnston wrote: > Tim Dudgeon wrote >> SELECT t1.id, t1.structure_id, t1.batch_id, >> t1.property_id, t1.property_data >> FROM chemcentral.structure_props t1 >> JOIN chemcentral.structure_props t2 ON t1.id = t2.id >> WHERE t2.structure_id IN (SELECT structure_id FROM >> chemcentral.structure_props WHERE property_id = 643413) >> AND t1.property_id IN (1, 643413, 1106201) >> ; > What about: > > SELECT t1.id, t1.structure_id, t1.batch_id, t1.property_id, t1.property_data > FROM chemcentral.structure_props t1 > JOIN ( > SELECT DISTINCT super.id FROM chemcentral.structure_props super > WHERE super.structure_id IN ( > SELECT sub.structure_id > FROM chemcentral.structure_props sub > WHERE sub.property_id = 643413 > ) > ) t2 ON (t1.id = t2.id) > WHERE t1.property_id IN (1, 643413, 1106201) > ; > > ? > > I do highly suggest using column table prefixes everywhere in this kind of > query... > > Also, AND == INTERSECT so: > > SELECT ... FROM chemcentral.structure_props WHERE property_id IN > (1,643413,1106201) > INTERSECT DISTINCT > SELECT ... FROM chemcentral.structure_props WHERE structure_id IN (SELECT > ... WHERE property_id = 643413) > > You can even use CTE/WITH expressions and give these subqueries meaningful > names. > > David J. > > > > > -- > View this message in context: http://postgresql.nabble.com/slow-sub-query-problem-tp5827273p5827453.html > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > > -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql