[Solved] Take query true when one clause is false

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 `ps_auctions_rutcom` ADD INDEX `ps_rutcom_idx_active_finish_id_prod_finish` (`active`,`finish`,`id_product`,`finish_date`);
ALTER TABLE `ps_image` ADD INDEX `ps_image_idx_cover_id_product` (`cover`,`id_product`);
ALTER TABLE `ps_product_lang` ADD INDEX `ps_lang_idx_id_lang_id_product` (`id_lang`,`id_product`);
The optimized query:
SELECT
        pl.NAME,
        pl.id_product,
        pl.link_rewrite,
        i.id_image,
        ar.id_auctions_rutcom,
        ar.actual_bid_price,
        ar.start_price,
        ar.min_price,
        ar.buy_now_price,
        ar.finish_date 
    FROM
        ps_image i,
        ps_auctions_rutcom ar,
        ps_product_lang pl 
    WHERE
        ar.finish_date > '2013-07-26 11:18:15' 
        AND ar.start_date < '2013-07-26 11:18:15' 
        AND ar.active = 1 
        AND ar.finish = 0 
        AND pl.id_product = ar.id_product 
        AND i.id_product = ar.id_product 
        AND i.cover = 1 
        AND pl.id_lang = 6

Related Articles



* original question posted on StackOverflow here.