[Solved] Adding ORDER BY significantly slows down JOIN query even though all relevant columns are indexed. How can I make it faster?

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 Calling Functions With Indexed Columns (query line: 11): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `latitude` is indexed, the index won’t be used as it’s wrapped with the function `ACOS`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 11): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `longitude` is indexed, the index won’t be used as it’s wrapped with the function `ACOS`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  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.
  5. Use Equality Operator Over LIKE (modified query below): Equality operators (such as '\u003d') are usually better optimized and more readable. Prefer the equality operator when searching for a constant value such as `A`.
  6. Use Equality Operator Over LIKE (modified query below): Equality operators (such as '\u003d') are usually better optimized and more readable. Prefer the equality operator when searching for a constant value such as `X`.
  7. Use Equality Operator Over LIKE (modified query below): Equality operators (such as '\u003d') are usually better optimized and more readable. Prefer the equality operator when searching for a constant value such as `X-Y`.
  8. Use Numeric Column Types For Numeric Values (query line: 12): 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.
Optimal indexes for this query:
ALTER TABLE `Table1` ADD INDEX `table1_idx_col2_col3_col1` (`col2`,`col3`,`col1`);
The optimized query:
SELECT
        table1.*,
        table2.* 
    FROM
        Table1 AS table1 
    LEFT JOIN
        Table2 AS table2 USING (col1) 
    LEFT JOIN
        Table3 AS table3 USING (col1) 
    WHERE
        3963.191 * ACOS((SIN(PI() * $usersLatitude / 180) * SIN(PI() * table3.latitude / 180)) + (COS(PI() * $usersLatitude / 180) * COS(PI() * table3.latitude / 180) * COS(PI() * table3.longitude / 180 - PI() * 37.1092162 / 180))) <= 10 
        AND table1.col1 != '1' 
        AND table1.col2 = 'A' 
        AND (
            table1.col3 = 'X' 
            OR table1.col3 = 'X-Y'
        ) 
        AND (
            table2.col4 = 'Y' 
            OR table2.col5 = 'Y'
        )

Related Articles



* original question posted on StackOverflow here.