[Solved] Mysql left join take too much time

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the 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 Calling Functions With Indexed Columns (query line: 10): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `purchasing_date` is indexed, the index won’t be used as it’s wrapped with the function `DATE_SUB`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 10): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `purchasing_date` is indexed, the index won’t be used as it’s wrapped with the function `DATE_ADD`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid Calling Functions With Indexed Columns (query line: 21): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `purchasing_date` is indexed, the index won’t be used as it’s wrapped with the function `DATE_SUB`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  4. Avoid Calling Functions With Indexed Columns (query line: 21): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `purchasing_date` is indexed, the index won’t be used as it’s wrapped with the function `DATE_ADD`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  5. 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.
  6. 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'.
  7. Index Function Calls Using Generated Columns (modified query below): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index to optimize the search. Creating and indexing a generated column (supported in MySQL 5.7) will allow MySQL to optimize the search.
  8. Replace Left Join With Subquery (modified query below): The pattern of inflating the amount of data (using joins) and deflating (using GROUP BY) usually slows down queries. In this case, it can be avoided by moving some of the logic to the SELECT clause, and therefore removing some of the LEFT JOINs. In some cases, this transformation can lead to an obsolete GROUP BY clause, which can also be removed.
Optimal indexes for this query:
ALTER TABLE `purchasing_receipt` ADD INDEX `purchasing_receipt_idx_processing_id` (`processing_id`);
ALTER TABLE `transaction_log` ADD INDEX `transaction_log_idx_matched_transac_merchan_transac` (`matched_flag`,`transaction_amount`,`merchant_no`,`transaction_date`);
The optimized query:
SELECT
        p.purchasing_receipt_id,
        (SELECT
            CASE 
                WHEN count(t.transaction_id) = 1 THEN min(t.transaction_id) 
                ELSE NULL END 
FROM
transaction_log t 
WHERE
t.transaction_date BETWEEN DATE_SUB(p.purchasing_date, INTERVAL 2 MINUTE) AND DATE_ADD(p.purchasing_date, INTERVAL 2 MINUTE) 
AND t.transaction_amount = p.total_amount 
AND p.merchant_no = t.merchant_no 
AND t.matched_flag = 'N' LIMIT 1),
(SELECT
CASE 
    WHEN count(t.transaction_id) = 1 THEN 'A' 
    ELSE 'U' END 
FROM
transaction_log t 
WHERE
t.transaction_date BETWEEN DATE_SUB(p.purchasing_date, INTERVAL 2 MINUTE) AND DATE_ADD(p.purchasing_date, INTERVAL 2 MINUTE) 
AND t.transaction_amount = p.total_amount 
AND p.merchant_no = t.merchant_no 
AND t.matched_flag = 'N' LIMIT 1) 
FROM
purchasing_receipt p 
WHERE
p.processing_id = 783 
ORDER BY
NULL

Related Articles



* original question posted on StackOverflow here.