[Solved] Optimizing my sql query

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: 7): 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: 21): 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: 40): 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: 59): 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. Avoid Subqueries (query line: 78): 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.
  6. Avoid Subqueries (query line: 97): 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.
  7. Avoid Subqueries (query line: 116): 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.
  8. Avoid Subqueries (query line: 135): 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.
  9. Avoid Subqueries (query line: 154): 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.
  10. Avoid Subqueries (query line: 173): 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.
  11. 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.
Optimal indexes for this query:
ALTER TABLE `TABLE1` ADD INDEX `table1_idx_field1` (`Field1`);
The optimized query:
SELECT
        CONCAT('0 - ',
        M.MaxField1) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 < M.MaxField1 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1),
        ' - ',
        (M.MaxField1 * 2)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1
        ) 
        AND P.Field1 < (
            M.MaxField1 * 2
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 2),
        ' - ',
        (M.MaxField1 * 3)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 2
        ) 
        AND P.Field1 < (
            M.MaxField1 * 3
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 3),
        ' - ',
        (M.MaxField1 * 4)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 3
        ) 
        AND P.Field1 < (
            M.MaxField1 * 4
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 4),
        ' - ',
        (M.MaxField1 * 5)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 4
        ) 
        AND P.Field1 < (
            M.MaxField1 * 5
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 5),
        ' - ',
        (M.MaxField1 * 6)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 5
        ) 
        AND P.Field1 < (
            M.MaxField1 * 6
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 6),
        ' - ',
        (M.MaxField1 * 7)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 6
        ) 
        AND P.Field1 < (
            M.MaxField1 * 7
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 7),
        ' - ',
        (M.MaxField1 * 8)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 7
        ) 
        AND P.Field1 < (
            M.MaxField1 * 8
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 8),
        ' - ',
        (M.MaxField1 * 9)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 8
        ) 
        AND P.Field1 < (
            M.MaxField1 * 9
        ) 
    UNION
    ALL SELECT
        CONCAT((M.MaxField1 * 9),
        ' - ',
        (M.MaxField1 * 10)) AS 'Part',
        COUNT(*) 
    FROM
        TABLE1 P,
        (SELECT
            (MAX(TABLE1.Field1) * (1 / 10)) AS MaxField1 
        FROM
            TABLE1) AS M 
    WHERE
        P.Field1 >= (
            M.MaxField1 * 9
        )

Related Articles



* original question posted on StackOverflow here.