[Solved] Improve performance of the 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 Calling Functions With Indexed Columns (query line: 17): 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 `Jobname` is indexed, the index won’t be used as it’s wrapped with the function `concat`. 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: 17): 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 `validationtype` is indexed, the index won’t be used as it’s wrapped with the function `concat`. 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 Subqueries (query line: 18): 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: 26): 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. 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 `contentvalidation` ADD INDEX `contentvalidation_idx_appname_teststartdateti` (`appname`,`teststartdatetime`);
ALTER TABLE `contentvalidationjobdetails` ADD INDEX `contentvalidationj_idx_appname_result` (`appname`,`result`);
The optimized query:
SELECT
        DISTINCT contentvalidationjobdetails.JobName,
        contentvalidationjobdetails.ValidationType,
        contentvalidationjobdetails.AppName,
        contentvalidationjobdetails.Result,
        contentvalidationjobdetails.ResultType,
        contentvalidationjobdetails.ErrorWarningDetails,
        contentvalidationjobdetails.CvtStartDateTime 
    FROM
        contentvalidationjobdetails WITH (NOLOCK) 
    WHERE
        contentvalidationjobdetails.appname = @AppName 
        AND contentvalidationjobdetails.result = @Result 
        AND (
            CAST(contentvalidationjobdetails.cvtstartdatetime AS date) > @Date
        ) 
        AND concat(contentvalidationjobdetails.Jobname, contentvalidationjobdetails.validationtype) NOT IN (
            SELECT
                concat(Contentvalidationjobdetails.jobname,
                Contentvalidationjobdetails.validationtype) 
            FROM
                Contentvalidationjobdetails WITH (NOLOCK) 
            WHERE
                Contentvalidationjobdetails.appname = @AppName 
                AND Contentvalidationjobdetails.CVTStartDateTime = (
                    SELECT
                        TOP 1 contentvalidation.teststartdatetime 
                    FROM
                        contentvalidation 
                    WHERE
                        contentvalidation.appname = @AppName 
                        AND contentvalidation.Teststartdatetime <@ contentvalidation.Date 
                    ORDER BY
                        contentvalidation.teststartdatetime DESC
                )
            )

Related Articles



* original question posted on StackOverflow here.