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 1nt7tU-0002st-Ab for pgsql-sql@arkaria.postgresql.org; Mon, 23 May 2022 13:14:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nt7tT-0004bE-2x for pgsql-sql@arkaria.postgresql.org; Mon, 23 May 2022 13:14: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 1nt7tS-0004ac-P9 for pgsql-sql@lists.postgresql.org; Mon, 23 May 2022 13:14:42 +0000 Received: from mout.gmx.net ([212.227.15.18]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nt7tO-0005TN-Ly for pgsql-sql@lists.postgresql.org; Mon, 23 May 2022 13:14:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1653311676; bh=2GYNgjbsgqGXDq8YXz9Uv2zK/P8McL2pfDJVgxrAlKk=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:References:In-Reply-To; b=UhVVQAhG4y8RRO/R/qpDq6uOcYOZCQFG5JK7OChLKTBKZCO9S8pjDRAAJNglONvUK VJINKijuaVDBU+APLsCFDK6b4RY+4FWND6Tnj5Texj3x+rW7jhkyYaICni4HkxT3UN VIEg82p6BQWGfDwiA6wC7Mwi+XUOdJzo3uiJJpLA= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from frastr-raspi.localdomain ([84.179.150.154]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N63Ra-1nmV5Y0t4a-016Nms; Mon, 23 May 2022 15:14:36 +0200 Received: from frastr-dev (p54b3969a.dip0.t-ipconnect.de [84.179.150.154]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by frastr-raspi.localdomain (Postfix) with ESMTPSA id 5977C6092E; Mon, 23 May 2022 15:14:35 +0200 (CEST) Date: Mon, 23 May 2022 15:14:33 +0200 From: Frank Streitzig To: kimaidou Cc: pgsql-sql@lists.postgresql.org Subject: Re: Count child objects for each line of a table: LEFT JOIN, LATERAL JOIN or subqueries ? Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Provags-ID: V03:K1:DRxovQ842mqoEDJfR7rqW3huHJ6WWhijryRE0dGYTrIdLbl8Csv DbCxPQk57eusxO1OceK3XOZ/nLUhpzDnEsZKKMoI1sQdkH9pXkQp9PzNoy028H0tELeo3Xg zhqofxZFHzvhezEc+jYOkWcvU11d/H3yd3oHda3f04G9SYmWY7JC6sRFWV60p/MbXbLbRmd SZ/mmU7wIRYKxaJRWhwnQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:LiOHXmk7XM0=:rNT1nf+TIAvMHvlIWTqJkg mvr18oYUMuYZYHJYfnRbFkolQFb47nIfpXunCWqwR/u9/7yAT3uO6cVxJE/CoYTnefSAPPaIv IA1QBh59w2mhGrNq794MisCjm9Qt1pRSFax8Ao/F8dueZ87iL4qUkk2KdDH96wd8PlSeQt7S3 NgI24jCE0geXwyApnQ2qy/sEpcHqmWC7ZdQgiv294fFDu1Z+71e5zO4JqVOTbHdgDeva52Z9L hYilsWdMiaxQUnLGI+Foqstu2g1RV2ldoZvqdf/4CxDg3wh13HkEUiNo2OgUdx/jcww7oZiKA EwlEpyEUF/tf4LVe4fUTthoxnbjH8w8kRjsT+NcDXPlCCa6BKUh5tKqBcN/YVyGzD1qgro3mK m/90l8aV7p15rfk6OG8u0rq2NfXQbo8tYyTHIHYfcRj2+kb6IXAGfSeWv/++atjeb33v8V9ln 7c7t8GMwJFujIuUvUqqKmd0gB13OJulNkY6uj7twLQIdMkp3hbMA4aY49wCl3LChYzuZlVlTe 9kY63xfsz9cormoRbC78d/I8xzsKCZI/HP68dOiJfb0PmDT6jhR/4BZOqacCw+aeIuS//U1WO b5vUDQZ4aJMxalmA5LIc60mhj7hzZvksAFvCMs/sSBVzT2Qzo30h3PNLQDWs0fiH1ZvafkoL0 /kYD95+XF0jOVTi7rXsSedj1BpoSUT/m3kVAbD+RZVHStjNV0nWpI8024HzrfTOhPocuHUWQJ 8hHilvpY06zV9ff7vUosi1UDEdKXYs7u3KInTja5/8IGM3ebuNoESPctPb0RFQXWR3nmlLfvw FiL+N+Y3Cbm0CROTCu5Zf0GY8OFbVan40QGlx9Ye8iEbQsJBoiTQhl6p72dzFSktskXFsN2YD JG4AZkRxLK/PeKdhtVatIHPBiW2F1rhbDjFl9KPjf0Y0sNNTqtZcdRwvzfMGIw5fId9V4sCvS KyPWm0+i8JP5lCkyf0AFUzrlA+DSxxC5c5coohAfSVkhGUj6HVsJj+bpl/+VxvTYsASKws6nq 15CaIjxgqT+43vuYSTsyi5k8ev7cYxB8qmE7oXihMjugLYXxSO0JjzlCbMF/gMawLXMsKFNp8 MVhOnt2RgryqnlLXzzEENrjMRCVskzR3hPooXad50FSpGnesNE/G1iBoA== Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Am Mon, May 23, 2022 at 01:55:07PM +0200 schrieb kimaidou: > Hi list, > > I have a basic need, often encountered in spatial analysis: I have a lis= t > of cities, parks, childcare centres, schools. I need to count the number= of > items for each city (0 if no item exists for this city) > > I have tested 3 different SQL queries to achieve this goal: > > * one with several LEFT JOINS: http://sqlfiddle.com/#!17/fe902/3 > * one with sub-queries: http://sqlfiddle.com/#!17/fe902/4 > * one with several LATERAL JOINS: http://sqlfiddle.com/#!17/fe902/6 Hello, Cost of queries see link "View Execution Plan" in fiddle query 1: 134.62 query 2: 8522.32 query 3: 134.62 query 1 and 3 have wrong count in result (columns nb_school, nb_childcare, nb_park) My try has cost of 81.83 select c.* , coalesce(s.cnt,0) as cnt_school , s.schools , coalesce(cc.cnt,0) as cnt_childcare , cc.childcares , coalesce(p.cnt,0) as cnt_park , p.parks from city c left outer join (select fk_id_city, count(*) as cnt ,string_agg(name, ', ') AS schools from school group by fk_id_city) s on s.fk_id_city =3D c.id left outer join (select fk_id_city, count(*) as cnt ,string_agg(name, ', ') AS childcares from childcare group by fk_id_city) cc on cc.fk_id_city =3D c.id left outer join (select fk_id_city, count(*) as cnt ,string_agg(name, ', ') AS parks from park group by fk_id_city) p on p.fk_id_city =3D c.id order by c.id ; IMHO, but without a where clause, the cost will increase with the amount of data. Regards, Frank