[Solved] Mysql: \"where in\" but need duplicates too

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. 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.
Optimal indexes for this query:
ALTER TABLE `rec_category_lang` ADD INDEX `rec_lang_idx_id_lang_id_category` (`id_lang`,`id_category`);
ALTER TABLE `rec_image` ADD INDEX `rec_image_idx_cover_id_product` (`cover`,`id_product`);
ALTER TABLE `rec_product_lang` ADD INDEX `rec_lang_idx_id_lang_id_product` (`id_lang`,`id_product`);
ALTER TABLE `rec_specific_price` ADD INDEX `rec_price_idx_id_product` (`id_product`);
The optimized query:
SELECT
        p.id_product,
        p.price,
        c.link_rewrite AS catrew,
        i.id_image,
        l.link_rewrite,
        l.name,
        s.reduction,
        s.reduction_type 
    FROM
        rec_product p 
    JOIN
        (
            SELECT
                13 AS id_product 
            UNION
            ALL SELECT
                13 
            UNION
            ALL SELECT
                119
        ) AS T1 
            ON p.id_product = T1.id_product 
    LEFT JOIN
        rec_image i 
            ON i.id_product = p.id_product 
            AND i.cover = 1 
    LEFT JOIN
        rec_product_lang l 
            ON l.id_product = p.id_product 
            AND l.id_lang = 1 
    LEFT JOIN
        rec_category_lang c 
            ON c.id_category = p.id_category_default 
            AND c.id_lang = 1 
    LEFT JOIN
        rec_specific_price s 
            ON s.id_product = p.id_product

Related Articles



* original question posted on StackOverflow here.