简体   繁体   English

提高查询性能

[英]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. 我知道concat(jobname,validationtype)需要时间。 how to handle this. 如何处理。

Place the query in FROm section to be executed just once (not for each line in WHERE). 将查询放在FROm节中仅执行一次(而不是针对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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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