简体   繁体   中英

Improve performance of the query

My query is taking very long time.

select distinct JobName,
       ValidationType,
       AppName,
       Result,
       ResultType,
       ErrorWarningDetails,
       CvtStartDateTime 
  from contentvalidationjobdetails with (nolock)
 where appname=@AppName 
       and result=@Result 
       and (cast(cvtstartdatetime as date) > @Date )
       and concat(Jobname,validationtype) not in (
                  select concat(jobname,validationtype) 
                    from Contentvalidationjobdetails with (nolock) 
                   where appname = @AppName 
                         and CVTStartDateTime = (
                          select top 1 teststartdatetime 
                            from contentvalidation 
                           where appname=@AppName 
                                 and Teststartdatetime<@Date
                        order by teststartdatetime desc
                         )
           )

I know that the concat(jobname,validationtype) is taking time. how to handle this.

Place the query in FROm section to be executed just once (not for each line in WHERE). Add outer join and leave only records which has no joins.

select distinct JobName,
       ValidationType,
       AppName,
       Result,
       ResultType,
       ErrorWarningDetails,
       CvtStartDateTime 
  from contentvalidationjobdetails with (nolock)
       LEFT OUTER JOIN (
                  select concat(jobname,validationtype) cnt
                    from Contentvalidationjobdetails with (nolock) 
                   where appname = @AppName 
                         and CVTStartDateTime = (
                          select top 1 teststartdatetime 
                            from contentvalidation 
                           where appname=@AppName 
                                 and Teststartdatetime<@Date
                        order by teststartdatetime desc) sub ON concat(Jobname,validationtype)=sub.cnt

 where appname=@AppName 
       and result=@Result 
       and (cast(cvtstartdatetime as date) > @Date ))
  HAVING sub.cnt is null 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM