[Solved] How do I clear an Aurora MySQL 5.6 Table Metadata Lock Wait when there are no locking transactions?

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 LIKE Searches With Leading Wildcard (query line: 51): The database will not use an index when using like searches with a leading wildcard (e.g. '%INFORMATION_SCHEMA%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  2. Avoid OR Conditions By Using UNION (modified query below): In mosts cases, filtering using the OR operator cannot be applied using indexes. A more optimized alternative will be to split the query to two parts combined with a UNION clause, while each query holds one part of the original OR condition.
  3. Avoid Subqueries (query line: 28): 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. Avoid Subqueries (query line: 52): 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.
  5. Use UNION ALL instead of UNION (query line: 38): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
The optimized query:
SELECT
        information_schema_processlist_id,
        information_schema_processlist_time,
        information_schema_processlist_user,
        information_schema_processlist_host,
        information_schema_processlist_db,
        information_schema_processlist_command,
        information_schema_processlist_state,
        information_schema_processlist_info 
    FROM
        ((SELECT
            INFORMATION_SCHEMA.PROCESSLIST.ID AS information_schema_processlist_id,
            INFORMATION_SCHEMA.PROCESSLIST.TIME AS information_schema_processlist_time,
            INFORMATION_SCHEMA.PROCESSLIST.USER AS information_schema_processlist_user,
            INFORMATION_SCHEMA.PROCESSLIST.HOST AS information_schema_processlist_host,
            INFORMATION_SCHEMA.PROCESSLIST.DB AS information_schema_processlist_db,
            INFORMATION_SCHEMA.PROCESSLIST.COMMAND AS information_schema_processlist_command,
            INFORMATION_SCHEMA.PROCESSLIST.STATE AS information_schema_processlist_state,
            INFORMATION_SCHEMA.PROCESSLIST.INFO AS information_schema_processlist_info 
        FROM
            INFORMATION_SCHEMA.PROCESSLIST 
        WHERE
            INFORMATION_SCHEMA.PROCESSLIST.DB IS NOT NULL 
            AND (
                INFORMATION_SCHEMA.PROCESSLIST.INFO IS NULL
            ) 
            AND INFORMATION_SCHEMA.PROCESSLIST.TIME > (
                SELECT
                    information_schema.PROCESSLIST.TIME 
                FROM
                    information_schema.PROCESSLIST 
                WHERE
                    information_schema.PROCESSLIST.id = 1319084
            ) 
        ORDER BY
            INFORMATION_SCHEMA.PROCESSLIST.`TIME`) 
        UNION
        DISTINCT (SELECT
            INFORMATION_SCHEMA.PROCESSLIST.ID AS information_schema_processlist_id,
            INFORMATION_SCHEMA.PROCESSLIST.TIME AS information_schema_processlist_time,
            INFORMATION_SCHEMA.PROCESSLIST.USER AS information_schema_processlist_user,
            INFORMATION_SCHEMA.PROCESSLIST.HOST AS information_schema_processlist_host,
            INFORMATION_SCHEMA.PROCESSLIST.DB AS information_schema_processlist_db,
            INFORMATION_SCHEMA.PROCESSLIST.COMMAND AS information_schema_processlist_command,
            INFORMATION_SCHEMA.PROCESSLIST.STATE AS information_schema_processlist_state,
            INFORMATION_SCHEMA.PROCESSLIST.INFO AS information_schema_processlist_info 
        FROM
            INFORMATION_SCHEMA.PROCESSLIST 
        WHERE
            INFORMATION_SCHEMA.PROCESSLIST.DB IS NOT NULL 
            AND (INFORMATION_SCHEMA.PROCESSLIST.`INFO` NOT LIKE '%INFORMATION_SCHEMA%') 
            AND INFORMATION_SCHEMA.PROCESSLIST.TIME > (SELECT
                information_schema.PROCESSLIST.TIME 
            FROM
                information_schema.PROCESSLIST 
            WHERE
                information_schema.PROCESSLIST.id = 1319084) 
        ORDER BY
            INFORMATION_SCHEMA.PROCESSLIST.`TIME`)
    ) AS union1 
ORDER BY
    union1.information_schema_processlist_time

Related Articles



* original question posted on StackOverflow here.