[Solved] SQL - IN (Select ... ) Performance issue

EverSQL Database Performance Knowledge Base

SQL - IN (Select ... ) Performance issue

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

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Subqueries (query line: 11): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  3. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  4. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `table1` ADD INDEX `table1_idx_orderref_shipmentdate` (`OrderRef`,`ShipmentDate`);
ALTER TABLE `table1` ADD INDEX `table1_idx_orderref_client` (`OrderRef`,`Client`);
The optimized query:
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

Related Articles



* original question posted on StackOverflow here.