(MySQL, InnoDB) Database has the tables Client (id, name)
and Order (id, client_id, name, order_date)
.
I would like to query them to receive a list of Client
and one, latest Order
for each Client, or none if there is none for that Client (left join).
There are additional constraints, joins and columns on the selected rows, but they should not be getting in the way of the Client-Order join, which is direct.
The problem is, even with the constraints, it’s picking 1000+ rows from a 100 000+ row total selection, and it takes 15+ minutes to return. When I try to export it to a CSV file, it fails to do it after even an hour.
Here’s what I tried:
SELECT DISTINCT
Client.id as client_id,
Client.name as client_name,
Order.name as order
FROM
Client
LEFT JOIN
Order ON Order.id = (SELECT O2.id FROM Order O2
<... joins and clauses for side tables ...>
WHERE O2.orderDate >= '2021-01-01'
AND O2.orderDate <= '2022-01-01'
AND O2.client_id = Client.id
ORDER BY O2.id DESC
LIMIT 1)
<... joins and clauses for side tables ...>;
I’ve googled and the say subqueries are inefficient, so is there a way to query this without one and/or in a more efficient way?
Edit: I am using MySQL8 with DBeaver 22.2.2. Simply querying the data and outputting into the DBeaver window takes around 11 minutes. If I then right-click it and select "Export", the problems begin.
2
Answers
I would extract the latest order per client using a CTE, and then query that
I would use a left join to extract the max date then get the id of that record to join with orders again. there you go, Tested and working.
Metadata
Output
client order order_date
Ryan | Burger | 2023-03-09 00:00:00
Joanna | Spaghetti | 2023-03-10 00:00:00
Sbira | Cheese 2023-03-11 00:00:00
Jamal NULL NULL