[Solved] SQL - Query Running Very Slow

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 OFFSET In LIMIT Clause (query line: 16): OFFSET clauses can be very slow when used with high offsets (e.g. with high page numbers when implementing paging). Instead, use the following \u003ca target\u003d"_blank" href\u003d"http://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/"\u003eseek method\u003c/a\u003e, which provides better and more stable response rates.
  2. Avoid Selecting Unnecessary Columns (query line: 14): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  3. Avoid Subqueries (query line: 13): 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.
Optimal indexes for this query:
ALTER TABLE `beverages` ADD INDEX `beverages_idx_beverage_id` (`beverage_id`);
ALTER TABLE `event_options` ADD INDEX `event_options_idx_event_id` (`event_option_id`);
ALTER TABLE `events` ADD INDEX `events_idx_event_id` (`event_id`);
ALTER TABLE `features` ADD INDEX `features_idx_feature_id` (`feature_id`);
ALTER TABLE `foods` ADD INDEX `foods_idx_food_id` (`food_id`);
ALTER TABLE `space_requirements` ADD INDEX `space_requirements_idx_space_id` (`space_requirement_id`);
ALTER TABLE `styles` ADD INDEX `styles_idx_style_id` (`style_id`);
ALTER TABLE `types` ADD INDEX `types_idx_type_id` (`type_id`);
ALTER TABLE `venue_beverages` ADD INDEX `venue_beverages_idx_venue_id` (`venue_id`);
ALTER TABLE `venue_event_options` ADD INDEX `venue_options_idx_venue_id` (`venue_id`);
ALTER TABLE `venue_events` ADD INDEX `venue_events_idx_venue_id` (`venue_id`);
ALTER TABLE `venue_features` ADD INDEX `venue_features_idx_venue_id` (`venue_id`);
ALTER TABLE `venue_foods` ADD INDEX `venue_foods_idx_venue_id` (`venue_id`);
ALTER TABLE `venue_space_requirements` ADD INDEX `venue_requirements_idx_venue_id` (`venue_id`);
ALTER TABLE `venue_styles` ADD INDEX `venue_styles_idx_venue_id` (`venue_id`);
ALTER TABLE `venue_types` ADD INDEX `venue_types_idx_venue_id` (`venue_id`);
The optimized query:
SELECT
        DISTINCT venues.name,
        locations.location,
        events.event,
        types.type,
        foods.food,
        beverages.beverage,
        event_options.event_option,
        styles.style,
        space_requirements.space_requirement,
        features.feature 
    FROM
        (SELECT
            * 
        FROM
            venues v LIMIT $offset,
            $limit) venues 
    INNER JOIN
        locations 
            ON venues.location_id = locations.location_id 
    LEFT JOIN
        venue_events 
            ON venues.venue_id = venue_events.venue_id 
    LEFT JOIN
        events 
            ON events.event_id = venue_events.event_id 
    LEFT JOIN
        venue_types 
            ON venues.venue_id = venue_types.venue_id 
    LEFT JOIN
        types 
            ON types.type_id = venue_types.type_id 
    LEFT JOIN
        venue_foods 
            ON venues.venue_id = venue_foods.venue_id 
    LEFT JOIN
        foods 
            ON foods.food_id = venue_foods.food_id 
    LEFT JOIN
        venue_beverages 
            ON venues.venue_id = venue_beverages.venue_id 
    LEFT JOIN
        beverages 
            ON beverages.beverage_id = venue_beverages.beverage_id 
    LEFT JOIN
        venue_event_options 
            ON venues.venue_id = venue_event_options.venue_id 
    LEFT JOIN
        event_options 
            ON event_options.event_option_id = venue_event_options.event_option_id 
    LEFT JOIN
        venue_styles 
            ON venues.venue_id = venue_styles.venue_id 
    LEFT JOIN
        styles 
            ON styles.style_id = venue_styles.style_id 
    LEFT JOIN
        venue_space_requirements 
            ON venues.venue_id = venue_space_requirements.venue_id 
    LEFT JOIN
        space_requirements 
            ON space_requirements.space_requirement_id = venue_space_requirements.space_requirement_id 
    LEFT JOIN
        venue_features 
            ON venues.venue_id = venue_features.venue_id 
    LEFT JOIN
        features 
            ON features.feature_id = venue_features.feature_id

Related Articles



* original question posted on StackOverflow here.