Count child objects for each line of a table: LEFT JOIN, LATERAL JOIN or subqueries ?

From: kimaidou <kimaidou(at)gmail(dot)com>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Count child objects for each line of a table: LEFT JOIN, LATERAL JOIN or subqueries ?
Date: 2022-05-23 11:55:07
Message-ID: CAMKXKO6Sam3WrzCb12yRyW+=OT_7K9zz3MfAQy4PNywuqhkGTQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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ël

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Frank Streitzig 2022-05-23 13:14:33 Re: Count child objects for each line of a table: LEFT JOIN, LATERAL JOIN or subqueries ?
Previous Message Bharath Rupireddy 2022-05-10 03:56:47 Re: Set timeout just on a query?