[Solved] MySql trying to improve query performance but got error on a column

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 Correlated Subqueries (query line: 26): A correlated subquery is a subquery that contains a reference (column: id) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
  2. 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.
  3. Index Function Calls Using Generated Columns (modified query below): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index to optimize the search. Creating and indexing a generated column (supported in MySQL 5.7) will allow MySQL to optimize the search.
  4. Prefer Direct Join Over Joined Subquery (query line: 10): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, we recommend to replace subqueries with JOIN clauses.
Optimal indexes for this query:
ALTER TABLE `event_revisions` ADD INDEX `event_revisions_idx_year_date_event_id` (`year_start_date`,`event_id`);
ALTER TABLE `event_revisions` ADD INDEX `event_revisions_idx_approved_event_id_revision` (`approved`,`event_id`,`revision`);
ALTER TABLE `events` ADD INDEX `events_idx_enabled_deleted` (`enabled`,`deleted`);
ALTER TABLE `member_events` ADD INDEX `member_events_idx_event_id` (`event_id`);
The optimized query:
SELECT
        e.id AS event_id,
        er.name AS event_name,
        er.revision AS revision,
        er.start_date AS event_dates 
    FROM
        `events` AS e 
    INNER JOIN
        event_revisions AS er 
            ON (
                e.id = er.event_id
            ) 
    INNER JOIN
        member_events AS me 
            ON (
                me.event_id = e.id
            ) 
    WHERE
        (
            e.enabled = 1 
            AND e.deleted = 0
        ) 
        AND (
            er.year_start_date = YEAR(CURDATE()) 
            AND er.revision = (
                SELECT
                    MAX(er_tmp.revision) 
                FROM
                    event_revisions AS er_tmp 
                WHERE
                    er_tmp.approved = 1 
                    AND e.id = er_tmp.event_id
            )
        ) 
    GROUP BY
        event_id 
    ORDER BY
        event_dates

Related Articles



* original question posted on StackOverflow here.