[Solved] SQL Query performs slower for another mysql version?

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: 12): 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: 19): 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: 27): 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. 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'.
  5. Use Numeric Column Types For Numeric Values (query line: 38): 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.
The optimized query:
SELECT
        topquery.kind_id,
        topquery.image,
        topquery.id,
        userquery.user_name 
    FROM
        `order` AS topquery,
        `user` AS userquery 
    WHERE
        userquery.user_id = topquery.user_id 
        AND topquery.id IN (
            SELECT
                MIN(mainquery.id) 
            FROM
                `order` AS mainquery 
            WHERE
                mainquery.user_id != '$vUserId' 
                AND mainquery.id NOT IN (
                    SELECT
                        history.order_id 
                    FROM
                        history 
                    WHERE
                        history.user_id = '$vUserId'
                ) 
                AND mainquery.kind_id NOT IN (
                    SELECT
                        o.kind_id 
                    FROM
                        history h 
                    INNER JOIN
                        `order` o 
                            ON h.order_id = o.id 
                    WHERE
                        h.user_id = '$vUserId'
                ) 
                AND mainquery.actions > 0 
                AND mainquery.kind = '1' 
            GROUP BY
                mainquery.kind_id 
            ORDER BY
                NULL) 
            ORDER BY
                vip DESC LIMIT 35

Related Articles



* original question posted on StackOverflow here.