[Solved] What should i use instead of IN?

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. 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.
  2. Index Function Calls Using Generated Columns (modified query below): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index to optimize the search. Creating and indexing a generated column (supported in MySQL 5.7) will allow MySQL to optimize the search.
Optimal indexes for this query:
ALTER TABLE `devices` ADD INDEX `devices_idx_isdeleted_id` (`isDeleted`,`id`);
ALTER TABLE `goldenvariation` ADD INDEX `goldenvariation_idx_classtype_date_runda_deviceid` (`classType`,`date_rundate`,`deviceId`);
ALTER TABLE `goldenvariation` ADD INDEX `goldenvariation_idx_goldenvari_classtype_date_runda` (`goldenVariationType`,`classType`,`date_rundate`);
The optimized query:
SELECT
        DISTINCT devices1_.id AS id27_,
        devices1_.createdTime AS createdT2_27_,
        devices1_.deletedOn AS deletedOn27_,
        devices1_.deviceAlias AS deviceAl4_27_,
        devices1_.deviceName AS deviceName27_,
        devices1_.deviceTypeId AS deviceT21_27_,
        devices1_.equipmentVendor AS equipmen6_27_,
        devices1_.exceptionDetail AS exceptio7_27_,
        devices1_.hardwareVersion AS hardware8_27_,
        devices1_.ipAddress AS ipAddress27_,
        devices1_.isDeleted AS isDeleted27_,
        devices1_.loopBack AS loopBack27_,
        devices1_.modifiedTime AS modifie12_27_,
        devices1_.osVersion AS osVersion27_,
        devices1_.productModel AS product14_27_,
        devices1_.productName AS product15_27_,
        devices1_.routerType AS routerType27_,
        devices1_.rundate AS rundate27_,
        devices1_.serialNumber AS serialN18_27_,
        devices1_.serviceName AS service19_27_,
        devices1_.siteId AS siteId27_,
        devices1_.siteIdA AS siteIdA27_,
        devices1_.status AS status27_,
        devices1_.creator AS creator27_,
        devices1_.lastModifier AS lastMod25_27_ 
    FROM
        goldenvariation goldenconf0_ 
    INNER JOIN
        devices devices1_ 
            ON goldenconf0_.deviceId = devices1_.id CROSS 
    JOIN
        devices devices2_ 
    WHERE
        goldenconf0_.deviceId = devices2_.id 
        AND (
            goldenconf0_.classType = 'policy-options'
        ) 
        AND goldenconf0_.date_rundate = DATE('2014-04-14 00:00:00') 
        AND devices2_.isDeleted = 0 
        AND EXISTS (
            SELECT
                DISTINCT (goldenconf3_.deviceId) 
            FROM
                goldenvariation goldenconf3_ 
            WHERE
                (
                    goldenconf3_.goldenVariationType = 'MISMATCH'
                ) 
                AND (
                    goldenconf3_.classType = 'policy-options'
                ) 
                AND goldenconf0_.date_rundate = DATE('2014-04-14 00:00:00')
        ) 
        AND EXISTS (
            SELECT
                DISTINCT (goldenconf4_.deviceId) 
            FROM
                goldenvariation goldenconf4_ 
            WHERE
                (
                    goldenconf4_.goldenVariationType = 'MISSING'
                ) 
                AND (
                    goldenconf4_.classType = 'policy-options'
                ) 
                AND goldenconf0_.date_rundate = DATE('2014-04-14 00:00:00')
        )

Related Articles



* original question posted on StackOverflow here.