[Solved] MySQL subquery is very slow, it is fast if run separately

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: 17): 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. 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.
  3. Use Numeric Column Types For Numeric Values (query line: 28): Referencing a numeric value (e.g. 60960) 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.
  4. Use Numeric Column Types For Numeric Values (query line: 29): Referencing a numeric value (e.g. 60986) 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 `data` ADD INDEX `data_idx_access_fk` (`access_point_id_name_fk`);
ALTER TABLE `meta` ADD INDEX `meta_idx_sensor_name_id` (`sensor_name`,`id`);
ALTER TABLE `prop` ADD INDEX `prop_idx_proid` (`proid`);
ALTER TABLE `string` ADD INDEX `string_idx_access_value_timebase` (`access_point_id_value`,`timebase`);
The optimized query:
SELECT
        esm1.prop_id,
        epc.propertyname,
        esm1.thermostat_id,
        esm1.thermostat_name,
        count(esm1.thermostat_id) * 5 AS minutes 
    FROM
        meta esm1 
    INNER JOIN
        data AS esdn 
            ON esm1.id = esdn.access_point_id_name_fk 
    INNER JOIN
        prop AS epc 
            ON epc.proid = esm1.prop_id 
    INNER JOIN
        (
            SELECT
                (esds.timebase) AS tb,
                (esm2.thermostat_id) AS tid 
            FROM
                string esds 
            INNER JOIN
                meta esm2 
                    ON esds.access_point_id_name_fk = esm2.id 
            WHERE
                esm2.sensor_name = 'zoneClimate' 
                AND esds.access_point_id_value = 'Occupied' 
                AND esds.timebase >= '60960' 
                AND esds.timebase <= '60986'
        ) AS esds_tmp 
            ON esds_tmp.tb = esdn.timebase 
            AND esds_tmp.tid = esm1.thermostat_id

Related Articles



* original question posted on StackOverflow here.