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: 38): 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 `AP_1028` is indexed, the index won’t be used as it’s wrapped with the function `SUBSTR`. 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: 11): 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 `AP_1028` is indexed, the index won’t be used as it’s wrapped with the function `SUBSTR`. 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'.
- 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.
- 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.
- Use Numeric Column Types For Numeric Values (query line: 40): Referencing a numeric value (e.g. 2000) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 11): Referencing a numeric value (e.g. 2000) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 13): Referencing a numeric value (e.g. 994) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
ALTER TABLE `AI_636` ADD INDEX `ai_636_idx_parent_id` (`PARENT_ID`);
ALTER TABLE `AI_665` ADD INDEX `ai_665_idx_ap_1033_ap_1030_ap_3222` (`AP_1033`,`AP_1030`,`AP_3222`);
ALTER TABLE `AI_665` ADD INDEX `ai_665_idx_ap_1033_ap_3222` (`AP_1033`,`AP_3222`);
The optimized query:
SELECT
AI_636.PARENT_ID AS PART,
MAX(b.AP_1036) AS ESTEND,
(SELECT
MAX(a.AP_3222) AS ACTEND
FROM
AI_665 a
WHERE
(
a.AP_1033 = AI_636.PARENT_ID
AND SUBSTR(a.AP_1028, 1, 4) >= '2000'
AND a.AP_1030 NOT IN (
'994'
)
AND (
NOT EXISTS (
SELECT
1
FROM
AI_665 AS AI_6651
WHERE
(
AI_6651.AP_3222 IS NULL
)
AND (
a.AP_1033 = AI_6651.AP_1033
)
)
)
) LIMIT 1
) AS ACTEND
FROM
AI_636
JOIN
AI_665 b
ON (
b.AP_1033 = AI_636.PARENT_ID
AND SUBSTR(b.AP_1028,
1,
4) >= '2000')
GROUP BY
AI_636.PARENT_ID
ORDER BY
NULL