[Solved] MySQL rand() function in ORDER BY

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 Subqueries (query line: 16): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Avoid Subqueries (query line: 33): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  3. Avoid Subqueries (query line: 49): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  4. 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.
  5. 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'.
Optimal indexes for this query:
ALTER TABLE `dtb_order` ADD INDEX `dtb_order_idx_order_id_customer_id` (`order_id`,`customer_id`);
ALTER TABLE `dtb_order_detail` ADD INDEX `dtb_detail_idx_product_id` (`product_id`);
ALTER TABLE `dtb_products` ADD INDEX `dtb_products_idx_product_id` (`product_id`);
ALTER TABLE `dtb_products` ADD INDEX `dtb_products_idx_del_flg_status` (`del_flg`,`status`);
ALTER TABLE `dtb_products_class` ADD INDEX `dtb_class_idx_product_id_price01_price02` (`product_id`,`price01`,`price02`);
The optimized query:
SELECT
        order_detail.product_id,
        dtb_products.name,
        dtb_products.main_list_image,
        dtb_products_price.price01_min,
        dtb_products_price.price01_max,
        dtb_products_price.price02_min,
        dtb_products_price.price02_max,
        dtb_products.limited_flag,
        dtb_products.limited_start_date,
        dtb_products.limited_end_date 
    FROM
        dtb_order 
    INNER JOIN
        (
            SELECT
                dtb_order_2.customer_id 
            FROM
                dtb_order AS dtb_order_2 
            INNER JOIN
                dtb_order_detail 
                    ON dtb_order_detail.order_id = dtb_order_2.order_id 
            WHERE
                dtb_order_detail.product_id = 1256 
            GROUP BY
                dtb_order_2.customer_id 
            ORDER BY
                NULL
        ) AS CUSTOMER 
            ON dtb_order.customer_id = CUSTOMER.customer_id 
    INNER JOIN
        (
            SELECT
                dtb_order_detail.product_id,
                dtb_order_detail.order_id 
            FROM
                dtb_order_detail 
            INNER JOIN
                dtb_products 
                    ON dtb_products.product_id = dtb_order_detail.product_id 
            WHERE
                dtb_order_detail.product_id <> 1256 
                AND dtb_products.del_flg = 0 
                AND dtb_products.status = 1
        ) AS order_detail 
            ON order_detail.order_id = dtb_order.order_id 
    LEFT JOIN
        (
            SELECT
                dtb_products_class.product_id AS product_id_sub,
                MIN(dtb_products_class.price01) AS price01_min,
                MAX(dtb_products_class.price01) AS price01_max,
                MIN(dtb_products_class.price02) AS price02_min,
                MAX(dtb_products_class.price02) AS price02_max 
            FROM
                dtb_products_class 
            GROUP BY
                dtb_products_class.product_id 
            ORDER BY
                NULL
        ) AS dtb_products_price 
            ON order_detail.product_id = dtb_products_price.product_id_sub 
    LEFT JOIN
        dtb_products 
            ON dtb_products.product_id = order_detail.product_id 
    GROUP BY
        order_detail.product_id 
    ORDER BY
        rand() LIMIT 8

Related Articles



* original question posted on StackOverflow here.