2014-03-13 88 views
0

我的查询需要很长时间。提高查询性能

select distinct JobName, 
     ValidationType, 
     AppName, 
     Result, 
     ResultType, 
     ErrorWarningDetails, 
     CvtStartDateTime 
    from contentvalidationjobdetails with (nolock) 
where [email protected] 
     and [email protected] 
     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 [email protected] 
           and Teststartdatetime<@Date 
         order by teststartdatetime desc 
         ) 
      ) 

我知道concat(jobname,validationtype)需要时间。如何处理这个。

+0

是的,它是sql服务器 –

+0

我想找到所选日期和日期以上的新作业名称。我连接了jobname和validationtype,因为它们形成了唯一的值 –

+0

您是否试图显示执行计划? – Axarydax

回答

1

将查询放在FROm节只执行一次(而不是每行都在WHERE中)。添加外连接并仅保留没有连接的记录。

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 [email protected] 
           and Teststartdatetime<@Date 
         order by teststartdatetime desc) sub ON concat(Jobname,validationtype)=sub.cnt 

where [email protected] 
     and [email protected] 
     and (cast(cvtstartdatetime as date) > @Date)) 
    HAVING sub.cnt is null