Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xr9Me-0002Gz-TN for pgsql-sql@arkaria.postgresql.org; Wed, 19 Nov 2014 17:48:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xr9Me-00037o-9h for pgsql-sql@arkaria.postgresql.org; Wed, 19 Nov 2014 17:48:24 +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 1Xr1Xe-0002Vb-9M for pgsql-sql@postgresql.org; Wed, 19 Nov 2014 09:27:14 +0000 Received: from mail-wi0-x233.google.com ([2a00:1450:400c:c05::233]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Xr1Xa-0002aY-9l for pgsql-sql@postgresql.org; Wed, 19 Nov 2014 09:27:12 +0000 Received: by mail-wi0-f179.google.com with SMTP id ex7so1125752wid.6 for ; Wed, 19 Nov 2014 01:27:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:references:from:content-type:in-reply-to:message-id:date:to :content-transfer-encoding:mime-version; bh=GKEDCldyIOrbtQ6WWoY1pysuuXrKJqb7+X7JrHQt1m8=; b=L3V8mGh8WDkxXyjFzd7oLmsrBl3YvRkmVuHQ0Ohct3TuPbmLgG3Wq7B1Gvax1CLrh/ JnTxP37MS1XkFtvx5JQdVGbGC9NCA4WhGN8yonqPcU6u0KXQKKdXPCk2uWn7nHyroNva KSrYTGvpkhXaS2sYZg4lgkZ8C3tyjHb0yKxTGXj2W+5BIok3togiTsn5MrmyKaMKzde2 Zyv0d70wX657bPFLjENdwpEy1dxxKDTBzFRvF4+wtdPmqdr7RILcqxXEnzw8F38rDLQF /Ofiu8is0Qiiuy7xGn1p2NbEQ9Zq35M5mA5q6XgYdpHCIQ7hmxHwwWAL1oOH1I/d3uZx pvrQ== X-Received: by 10.180.101.102 with SMTP id ff6mr11814761wib.34.1416389227891; Wed, 19 Nov 2014 01:27:07 -0800 (PST) Received: from [192.168.71.121] (94-21-103-248.pool.digikabel.hu. [94.21.103.248]) by mx.google.com with ESMTPSA id u13sm1391025wiv.10.2014.11.19.01.27.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 19 Nov 2014 01:27:07 -0800 (PST) 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> From: daku.sandor@gmail.com Content-Type: text/plain; charset=us-ascii X-Mailer: iPad Mail (12B410) In-Reply-To: <1416361021024-5827453.post@n5.nabble.com> Message-Id: <653A2F06-93F6-4FA1-BBE8-9353BB427503@gmail.com> Date: Wed, 19 Nov 2014 10:27:08 +0100 To: "pgsql-sql@postgresql.org" Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (1.0) 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 Slightly off: I prefer "exists" to "join" if it's possible while on the list I almost nev= er see any answer that uses "exists". Is my exists fixation is some kind of= bad practice? Sandor Daku > On 19 Nov 2014, at 02:37, David G Johnston w= rote: >=20 > Tim Dudgeon wrote >> SELECT t1.id, t1.structure_id, t1.batch_id,=20 >> t1.property_id, t1.property_data >> FROM chemcentral.structure_props t1 >> JOIN chemcentral.structure_props t2 ON t1.id =3D t2.id=20 >> WHERE t2.structure_id IN (SELECT structure_id FROM=20 >> chemcentral.structure_props WHERE property_id =3D 643413) >> AND t1.property_id IN (1, 643413, 1106201) >> ; >=20 > What about: >=20 > SELECT t1.id, t1.structure_id, t1.batch_id, t1.property_id, t1.property_d= ata > 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 =3D 643413 > ) > ) t2 ON (t1.id =3D t2.id)=20 > WHERE t1.property_id IN (1, 643413, 1106201) > ; >=20 > ? >=20 > I do highly suggest using column table prefixes everywhere in this kind of > query... >=20 > Also, AND =3D=3D INTERSECT so: >=20 > 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 =3D 643413) >=20 > You can even use CTE/WITH expressions and give these subqueries meaningful > names. >=20 > David J. >=20 >=20 >=20 >=20 > -- > 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. >=20 >=20 > --=20 > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql