[Solved] How Can I Optimize an SQL Query With Multiple Conditions and Inner Join

EverSQL Database Performance Knowledge Base

How Can I Optimize an SQL Query With Multiple Conditions and Inner Join

How can I optimize the SQL query below? It takes too much time. This is used to select from a very large astronomical table (a few hundred million objects), and this query runs over 12 hours which is more than the maximum allow query time. It appears that adding more restrictions via additional conditions doesn't help at all.

Your help is much appreciated!

select AVG(o.zMeanPSFMagStd) into mydb.z1819 
from MeanObject as o 
join ObjectThin ot on ot.ObjID=o.ObjID 
inner join StackObjectAttributes soa on soa.objID=o.objid 
where o.zMeanPSFMag>=18 
    and o.zMeanPSFMag<19 
    and o.zQfPerfect > 0.85 
    and (ot.qualityFlag & 1) = 0 
    and (ot.qualityFlag & 2) = 0 
    and o.zMeanPSFMagErr <> -999 
    and o.zMeanPSFMagStd <> -999 
    and ot.nz > 10 and soa.zpsfMajorFWHM < 6  
    and soa.zpsfMinorFWHM/nullif(soa.zpsfMajorFWHM,0) > 0.65 
    and soa.zpsfFlux/nullif(soa.zpsfFluxErr,0)>5 
    and (ot.b>10 or ot.b<-10) 
    and (ot.raMean>0 and ot.raMean<180) 

How to optimize this SQL query?

The following recommendations will help you in your 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: 25): 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 `zpsfMajorFWHM` is indexed, the index won’t be used as it’s wrapped with the function `nullif`. 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: 26): 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 `zpsfFluxErr` is indexed, the index won’t be used as it’s wrapped with the function `nullif`. 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. 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 `MeanObject` ADD INDEX `meanobject_idx_zmeanpsfmag` (`zMeanPSFMag`);
ALTER TABLE `ObjectThin` ADD INDEX `objectthin_idx_objid_nz` (`ObjID`,`nz`);
ALTER TABLE `StackObjectAttributes` ADD INDEX `stackobjectattribu_idx_objid_zpsfmajorfwhm` (`objID`,`zpsfMajorFWHM`);
The optimized query:
SELECT
        AVG(o.zMeanPSFMagStd) INTO mydb.z1819 
    FROM
        MeanObject AS o 
    JOIN
        ObjectThin ot 
            ON ot.ObjID = o.ObjID 
    INNER JOIN
        StackObjectAttributes soa 
            ON soa.objID = o.objid 
    WHERE
        o.zMeanPSFMag >= 18 
        AND o.zMeanPSFMag < 19 
        AND o.zQfPerfect > 0.85 
        AND (
            ot.qualityFlag & 1
        ) = 0 
        AND (
            ot.qualityFlag & 2
        ) = 0 
        AND o.zMeanPSFMagErr <> -999 
        AND o.zMeanPSFMagStd <> -999 
        AND ot.nz > 10 
        AND soa.zpsfMajorFWHM < 6 
        AND soa.zpsfMinorFWHM / nullif(soa.zpsfMajorFWHM, 0) > 0.65 
        AND soa.zpsfFlux / nullif(soa.zpsfFluxErr, 0) > 5 
        AND (
            ot.b > 10 
            OR ot.b < -10
        ) 
        AND (
            ot.raMean > 0 
            AND ot.raMean < 180
        )

Related Articles



* original question posted on StackOverflow here.