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:
- 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'.
- Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `mage_review_entity_summary`) were detected in the query. Removing them will result in a performance improvement. In some cases, JOINs become redundant after an optimization is applied, such as when converting OR conditions to a UNION clause.
- Use Numeric Column Types For Numeric Values (query line: 13): Referencing a numeric value (e.g. 2) 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: 17): Referencing a numeric value (e.g. 1) 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: 22): Referencing a numeric value (e.g. 154) 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: 25): Referencing a numeric value (e.g. 70) 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: 30): Referencing a numeric value (e.g. 153) 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: 31): Referencing a numeric value (e.g. 1) 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 `mage_catalog_category_product_index` ADD INDEX `mage_category_idx_store_categor_product_visibil` (`store_id`,`category_id`,`product_id`,`visibility`);
ALTER TABLE `mage_catalog_product_index_eav` ADD INDEX `mage_product_idx_attribu_store_entity_value` (`attribute_id`,`store_id`,`entity_id`,`value`);
ALTER TABLE `mage_catalog_product_index_price` ADD INDEX `mage_product_idx_website_id_customer_entity_id` (`website_id`,`customer_group_id`,`entity_id`);
The optimized query:
SELECT
`width_idx`.`value`,
COUNT(DISTINCT width_idx.entity_id) AS `count`
FROM
`mage_catalog_product_flat_1` AS `e`
INNER JOIN
`mage_catalog_category_product_index` AS `cat_index`
ON cat_index.product_id = e.entity_id
AND cat_index.store_id = 1
AND cat_index.visibility IN (
2,
4)
AND cat_index.category_id = '2'
INNER JOIN
`mage_catalog_product_index_price` AS `price_index`
ON price_index.entity_id = e.entity_id
AND price_index.website_id = '1'
AND price_index.customer_group_id = 0
INNER JOIN
`mage_catalog_product_index_eav` AS `gender_idx`
ON gender_idx.entity_id = e.entity_id
AND gender_idx.attribute_id = '154'
AND gender_idx.store_id = 1
AND gender_idx.value IN (
'70'
)
INNER JOIN
`mage_catalog_product_index_eav` AS `width_idx`
ON width_idx.entity_id = e.entity_id
AND width_idx.attribute_id = '153'
AND width_idx.store_id = '1'
GROUP BY
`width_idx`.`value`
ORDER BY
NULL