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 1nt6eh-0006pu-W5 for pgsql-sql@arkaria.postgresql.org; Mon, 23 May 2022 11:55:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nt6eg-0004hx-Ie for pgsql-sql@arkaria.postgresql.org; Mon, 23 May 2022 11:55:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nt6eg-0004ho-9r for pgsql-sql@lists.postgresql.org; Mon, 23 May 2022 11:55:22 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nt6ed-0001TM-Vm for pgsql-sql@lists.postgresql.org; Mon, 23 May 2022 11:55:21 +0000 Received: by mail-ed1-x531.google.com with SMTP id j4so14356011edq.6 for ; Mon, 23 May 2022 04:55:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=WhrbEfQOBcjNcDtBhbjhG3sLpnW8TlNExfZd5ZtASlE=; b=ZZf9pas4KCbgiqbYfu9WlLnRTPDKK0Di5p3YsIulhe4p/5dHM1jf5DBbulegaE3Fqp ijj8BszSsEeqItxCpVY+CzS6+2VVB5rpbUvOSqPG0XldQ9DWfqzEDN7xDT2XI24Ticlz qtUJLdJDfrg/Pkloa46NRphhdqVw8pI4XE1SGSk3KbvFEO88Sk4gOIh2oQdmmwu4yWW8 kKtip/T+vFL3m6Ef5Y0WoppfP67qlfHP1d4bQA+5cnAGW7BJ8vVYdR0lZfaCIayPn7lF 7hXq1AvDHiU/FVYXSj0CssGVIqIvUN8lUC7EVCKmjj5Tv8U+7pLkxESSQP71Ef1G1Vo+ 9YBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=WhrbEfQOBcjNcDtBhbjhG3sLpnW8TlNExfZd5ZtASlE=; b=IaO2tHRg/F1Z9V0BYfKDm6Uzdp5DfnwIVB71VxNpsLtTnvacvP/CxzNojf/6fI5nb7 cI08vuzgO/SObNZGwvWjfPJETJbDiLKbVK/sJm/bEPM72+v8eeab55Ugjt/iIS5In1ew wZ1PzCrJIo7L8KHaFm91xrG2+Bq+HKabOQEFCOvQco47ehTZqZch7Bxp2vxGUMlBZd6y UjEFWEn+4b5cto5Aq/sOb8Q/jDwgqF/5Tx09KRoKAhu+nXHHl4TqYlJ3LYWh0JjPfNq9 gjHI2gvJDS9bxK0faPOJmN+sxco16XfjDS+frS+G/tW1rWD3Sj1NYd5DU9sNzszn8x/t CoAw== X-Gm-Message-State: AOAM530WA1zfRQA0Lp/HlgOnQf6cJUBxfBvhS7eErztnRjN6VYf4HxUM gPCxWJTFTMjWkRBiEcn/LUJ03Qi3HF40bEigZPc2PJ7HS84= X-Google-Smtp-Source: ABdhPJx+P1oMYruIaPgT1XowBjHTcqdNfJ6Vyh59BoXf1CdM1VjP6Ql4u/X0j8LkRF+51963OmLDO1QHw3Dos+H20Og= X-Received: by 2002:a05:6402:4381:b0:42b:2dc4:bf16 with SMTP id o1-20020a056402438100b0042b2dc4bf16mr15835722edc.73.1653306918663; Mon, 23 May 2022 04:55:18 -0700 (PDT) MIME-Version: 1.0 From: kimaidou Date: Mon, 23 May 2022 13:55:07 +0200 Message-ID: Subject: Count child objects for each line of a table: LEFT JOIN, LATERAL JOIN or subqueries ? To: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000977aae05dfac83e7" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000977aae05dfac83e7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi list, I have a basic need, often encountered in spatial analysis: I have a list 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 I love the first one, with LEFT JOINS, because it is concise, seems simple, and allows querying easily more than one aggregated field from the child items. But we need to use the DISTINCT clause inside the aggregation functions (count, string_agg), in order to count each child only once. The one with the subqueries seems the more common way (I have seen it a lot) but I find it cumbersome, and I guess it won't scale well for bigger datasets. The one with the LATERAL joins seems overcomplicated, but I probably missed some easier way to use the lateral join. I do not know well how the LATERAL differs from the subqueries... I would like to have your opinion on this scenario. What is the best query for this use case, considering the fact that it should perform well on heavier datasets (1 million cities and thousands of children). NB: I used SQL Fiddle to help everyone see the data and SQL queries. Not sure if everyone can modify it or not. Please try to keep the 3 example unchanged. It seems SQL Fiddle has not been update since at least 2018, so PostgreSQL version is 9.6. Regards Micha=C3=ABl --000000000000977aae05dfac83e7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi list,

I have a basic need= , often encountered in spatial analysis: I have a list of cities, parks, ch= ildcare centres, schools. I need to count the number of items for each city= (0 if no item exists for this city)

I have te= sted 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/fe9= 02/4
* one with several LATERAL JOINS: http://sqlfiddle.com/#!17/fe902/6





--000000000000977aae05dfac83e7--