[Solved] Query is very very slow on MariaDB

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 Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. Remove Redundant Left Joins (modified query below): Redundant LEFT JOINs (e.g. `core_url_rewrite`) 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.
  3. Use Numeric Column Types For Numeric Values (query line: 27): 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.
  4. Use Numeric Column Types For Numeric Values (query line: 34): Referencing a numeric value (e.g. 96) 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.
  5. Use Numeric Column Types For Numeric Values (query line: 45): Referencing a numeric value (e.g. 102) 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.
  6. Use Numeric Column Types For Numeric Values (query line: 56): Referencing a numeric value (e.g. 313) 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.
  7. Use Numeric Column Types For Numeric Values (query line: 67): Referencing a numeric value (e.g. 353) 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.
  8. Use Numeric Column Types For Numeric Values (query line: 104): 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.
  9. Use Numeric Column Types For Numeric Values (query line: 113): 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.
  10. Use Numeric Column Types For Numeric Values (query line: 118): Referencing a numeric value (e.g. 19) 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.
  11. Use Numeric Column Types For Numeric Values (query line: 128): 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.
  12. Use Numeric Column Types For Numeric Values (query line: 133): 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.
The optimized query:
SELECT
        `e`.*,
        `at_status`.`value` AS `status`,
        `at_visibility`.`value` AS `visibility`,
        `at_quality_score`.`value` AS `quality_score`,
        `at_exportable_for_idealo`.`value` AS `exportable`,
        `stock`.`qty`,
        `stock`.`is_in_stock`,
        `stock`.`manage_stock`,
        `stock`.`use_config_manage_stock`,
        `stock`.`min_qty`,
        `stock`.`min_sale_qty`,
        MAX(DISTINCT request_path) AS `request_path`,
        `cpsl`.`parent_id`,
        `categories`.*,
        `categories_parent`.*,
        GROUP_CONCAT(DISTINCT categories_index.category_id) AS `categories_ids`,
        `price_index`.`min_price`,
        `price_index`.`max_price`,
        `price_index`.`tier_price`,
        `price_index`.`final_price` 
    FROM
        `catalog_product_entity` AS `e` 
    INNER JOIN
        `catalog_product_website` AS `product_website` 
            ON product_website.product_id = e.entity_id 
            AND product_website.website_id = '1' 
    INNER JOIN
        `catalog_product_entity_int` AS `at_status` 
            ON (
                `at_status`.`entity_id` = `e`.`entity_id`
            ) 
            AND (
                `at_status`.`attribute_id` = '96'
            ) 
            AND (
                `at_status`.`store_id` = 0
            ) 
    INNER JOIN
        `catalog_product_entity_int` AS `at_visibility` 
            ON (
                `at_visibility`.`entity_id` = `e`.`entity_id`
            ) 
            AND (
                `at_visibility`.`attribute_id` = '102'
            ) 
            AND (
                `at_visibility`.`store_id` = 0
            ) 
    INNER JOIN
        `catalog_product_entity_varchar` AS `at_quality_score` 
            ON (
                `at_quality_score`.`entity_id` = `e`.`entity_id`
            ) 
            AND (
                `at_quality_score`.`attribute_id` = '313'
            ) 
            AND (
                `at_quality_score`.`store_id` = 0
            ) 
    INNER JOIN
        `catalog_product_entity_int` AS `at_exportable_for_idealo` 
            ON (
                `at_exportable_for_idealo`.`entity_id` = `e`.`entity_id`
            ) 
            AND (
                `at_exportable_for_idealo`.`attribute_id` = '353'
            ) 
            AND (
                `at_exportable_for_idealo`.`store_id` = 0
            ) 
    LEFT JOIN
        `cataloginventory_stock_item` AS `stock` 
            ON stock.product_id = e.entity_id 
    LEFT JOIN
        `catalog_product_super_link` AS `cpsl` 
            ON cpsl.product_id = e.entity_id 
    LEFT JOIN
        `catalog_category_product` AS `categories` 
            ON categories.product_id = e.entity_id 
    LEFT JOIN
        `catalog_category_product` AS `categories_parent` 
            ON categories_parent.product_id = cpsl.parent_id 
    LEFT JOIN
        `catalog_category_product_index` AS `categories_index` 
            ON (
                (
                    categories_index.category_id = categories.category_id 
                    AND categories_index.product_id = categories.product_id
                ) 
                OR (
                    categories_index.category_id = categories_parent.category_id 
                    AND categories_index.product_id = categories_parent.product_id
                )
            ) 
            AND categories_index.store_id = 1 
    LEFT JOIN
        `catalog_product_index_price` AS `price_index` 
            ON price_index.entity_id = e.entity_id 
            AND customer_group_id = 0 
            AND price_index.website_id = 1 
    WHERE
        (
            at_status.value = '1'
        ) 
        AND (
            `e`.`type_id` IN (
                'simple'
            )
        ) 
        AND (
            at_visibility.value IN (
                '1'
            )
        ) 
        AND (
            `e`.`attribute_set_id` IN (
                '19', '13', '4'
            )
        ) 
        AND (
            (
                at_quality_score.value = 'A'
            )
        ) 
        AND (
            (
                at_status.value = '1'
            )
        ) 
        AND (
            (
                at_exportable_for_idealo.value = '1'
            )
        )

Related Articles



* original question posted on StackOverflow here.