Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jf7LZ-0006xb-1C for pgsql-sql@arkaria.postgresql.org; Sat, 30 May 2020 19:40:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jf7LX-0004F6-Fl for pgsql-sql@arkaria.postgresql.org; Sat, 30 May 2020 19:40:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jf7LX-0004Ez-9L for pgsql-sql@lists.postgresql.org; Sat, 30 May 2020 19:40:43 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jf7LP-0004oh-H5 for pgsql-sql@postgresql.org; Sat, 30 May 2020 19:40:41 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by ringways.co.uk with esmtps (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.92.3) (envelope-from ) id 1jf7LN-0008ER-FX for pgsql-sql@postgresql.org; Sat, 30 May 2020 20:40:33 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: Re: left outer join with count Date: Sat, 30 May 2020 20:40:32 +0100 User-Agent: KMail/1.9.10 References: <202005291709.16647.gary.stainburn@ringways.co.uk> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <202005302040.32647.gary.stainburn@ringways.co.uk> X-Spam-Score: -49.8 (-------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie2.ringways.co.uk", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: On Saturday 30 May 2020 05:43:06 Gábor SZŰCS wrote: > Hello Gary, > > Depends on way too many things, but assuming > - You are concerned about cardinality of wd Vs SJ > - SJ.sj_id is unique > - there are necessary indexes in place > ... > Then I'd recommend subselect in SELECT clause, like: > > > select sj.*, sr.*, > (select count(sj_id) from work_documents wd where > wd.sj_id=SJ.sj_id) doc_count > from > service_jobs sj > left outer join service_receptions sr on sr.sr_id = sj.sj_sr_id; > > (Note, haven't verified syntax) > > Maybe if you could send explain plans and index info... [...] Content analysis details: (-49.8 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.1 SCORE_RCPTS Adding score for each recipient 1.0 MISSING_FROM Missing From: header 1.0 RING_SAFE No description available. List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Saturday 30 May 2020 05:43:06 G=C3=A1bor SZ=C5=B0CS wrote: > Hello Gary, >=20 > Depends on way too many things, but assuming > - You are concerned about cardinality of wd Vs SJ > - SJ.sj_id is unique > - there are necessary indexes in place > ... > Then I'd recommend subselect in SELECT clause, like: >=20 >=20 > select sj.*, sr.*, > (select count(sj_id) from work_documents wd where > wd.sj_id=3DSJ.sj_id) doc_count > from > service_jobs sj > left outer join service_receptions sr on sr.sr_id =3D sj.sj_sr_id; >=20 > (Note, haven't verified syntax) >=20 > Maybe if you could send explain plans and index info... Thank you for this. I ended up doing it as two separate queries. Seems to = be quicker.