[Solved] mysql left join takes too long

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: 32): 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: 17): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  3. 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.
  4. Reduce Impact Of Subqueries In Select Clause (modified query below): Subqueries in the SELECT clause will be executed once per each select row. Therefore, reducing the amount of selected rows in the FROM clause before getting to the SELECT clause will result in a performance improvement.
  5. Replace Left Join With Subquery (modified query below): The pattern of inflating the amount of data (using joins) and deflating (using GROUP BY) usually slows down queries. In this case, it can be avoided by moving some of the logic to the SELECT clause, and therefore removing some of the LEFT JOINs. In some cases, this transformation can lead to an obsolete GROUP BY clause, which can also be removed.
  6. Use Numeric Column Types For Numeric Values (query line: 25): Referencing a numeric value (e.g. 0) 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.
  7. Use Numeric Column Types For Numeric Values (query line: 26): Referencing a numeric value (e.g. 118697835834) 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.
  8. Use Numeric Column Types For Numeric Values (query line: 27): Referencing a numeric value (e.g. 118697835834) 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 `subscribers` ADD INDEX `subscribers_idx_suid` (`suid`);
ALTER TABLE `updates` ADD INDEX `updates_idx_deleted_id` (`deleted`,`id`);
ALTER TABLE `users` ADD INDEX `users_idx_uid` (`uid`);
The optimized query:
SELECT
        optimizedSub1.*,
        (SELECT
            usr.username AS `username` 
        FROM
            users AS usr 
        WHERE
            optimizedSub1.upd_uid = usr.uid LIMIT 1) AS `username`,
        (SELECT
            usr.profile_picture AS `profile_picture` 
        FROM
            users AS usr 
        WHERE
            optimizedSub1.upd_uid = usr.uid LIMIT 1) AS `profile_picture` 
    FROM
        (SELECT
            upd.*,
            upd.uid AS upd_uid 
        FROM
            updates AS upd 
        LEFT JOIN
            subscribers AS sub 
                ON upd.uid = sub.suid 
        WHERE
            upd.deleted = '0' && (
                upd.uid = '118697835834' 
                OR sub.uid = '118697835834'
            ) 
        GROUP BY
            upd.id 
        ORDER BY
            upd.date DESC LIMIT 0,
            15) AS optimizedSub1

Related Articles



* original question posted on StackOverflow here.