[Solved] MYSQL GROUP BY and ORDER BY not working together as expected

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.
  2. Use Numeric Column Types For Numeric Values (query line: 33): 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.
  3. Use Numeric Column Types For Numeric Values (query line: 40): 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.
Optimal indexes for this query:
ALTER TABLE `hotel_rate_detail` ADD INDEX `hotel_detail_idx_hotels_id` (`hotels_id`);
ALTER TABLE `hotel_week_days` ADD INDEX `hotel_days_idx_h_id` (`h_id`);
ALTER TABLE `hotels_list` ADD INDEX `hotels_list_idx_hotel_city_hotel_id_hotel_star` (`hotel_city`,`hotel_id`,`hotel_star`);
ALTER TABLE `hotels_list` ADD INDEX `hotels_list_idx_hotel_id` (`hotel_id`);
The optimized query:
SELECT
        h.hotel_id AS id,
        h.hotel_city AS city,
        h.hotel_name AS hotelname,
        h.hotel_star AS hotelcat,
        hwd.double_spl_rate,
        hwd.extra_bed_spl_rate,
        hwd.meal_plan_spl,
        hwd.third_party_rate,
        hwd.third_party_extra_bed,
        hwd.third_party_meal_plan,
        hwd.room_category,
        hrd.hotel_rate_from,
        hrd.hotel_rate_to 
    FROM
        hotels_list AS h 
    INNER JOIN
        hotel_rate_detail AS hrd 
            ON h.hotel_id = hrd.hotels_id 
    INNER JOIN
        hotel_week_days AS hwd 
            ON hrd.hotel_id = hwd.h_id 
    WHERE
        (
            (
                '2015-07-02' BETWEEN hrd.hotel_rate_from AND hrd.hotel_rate_to
            ) 
            OR (
                '2015-07-03' BETWEEN hrd.hotel_rate_from AND hrd.hotel_rate_to
            )
        ) 
        AND (
            h.hotel_city = '1'
        ) 
        AND (
            hwd.double_spl_rate != 0 
            OR hwd.third_party_rate != 0
        ) 
        AND (
            h.hotel_star <= '2'
        ) 
        AND h.hotel_id = 364 
    GROUP BY
        h.hotel_id 
    ORDER BY
        hwd.double_spl_rate,
        hwd.third_party_rate ASC

Related Articles



* original question posted on StackOverflow here.