For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
The optimization process and recommendations:
- 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.
- 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.
- 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.
- 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.
- 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.
- 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'.
- 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.
- 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