I know in (select...) stuff should be avoided but in my situation I couldn't find another way to do it.
Unfortunately, as the database is huge, I would like to find a better way. So could you please share your experience to optimise the query below ?
Here is an idea of the db
ID OrderRef Product OrderDate ShipmentDate Client
1 111 T-Shirt 1/1/2018 4/1/2018 Georges
2 111 Pull-Over 1/1/2018 6/1/2018 (Unknown)
3 222 Shoes 9/1/2018 15/1/2018 Austin
4 222 T-Shirt 9/1/2018 18/1/2018 (Unknown)
What I need to retrieve is :
Here is my code
SELECT t1.OrderRef, t1.Product, t1.OrderDate, t1.Client, t4.max_date
FROM table1 as t1
RIGHT JOIN (SELECT t2.OrderRef, max(t2.ShipmentDate) as max_date
FROM table1 as t2
WHERE t2.OrderRef in(
SELECT t3.OrderRef
FROM table1 as t3
WHERE t3.Client='Georges')
GROUP BY t2.OrderRef) as t4 on t2.OrderRef=t1.OrderRef
The right join is there to retrieve only the OrderReference linked to Georges, and the subquery to calculate the the Latest ShipmentDate
Thanks in advance for your help, Largo
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `table1` ADD INDEX `table1_idx_orderref_shipmentdate` (`OrderRef`,`ShipmentDate`);
ALTER TABLE `table1` ADD INDEX `table1_idx_orderref_client` (`OrderRef`,`Client`);
SELECT
t1.OrderRef,
t1.Product,
t1.OrderDate,
t1.Client,
t4.max_date
FROM
table1 AS t1
RIGHT JOIN
(
SELECT
t2.OrderRef,
max(t2.ShipmentDate) AS max_date
FROM
table1 AS t2
WHERE
EXISTS (
SELECT
1
FROM
table1 AS t31
WHERE
(
t31.Client = 'Georges'
)
AND (
t2.OrderRef = t31.OrderRef
)
)
GROUP BY
t2.OrderRef
ORDER BY
NULL) AS t4
ON t2.OrderRef = t1.OrderRef