从Where子句中删除SubQuery

时间:2017-09-15 12:16:02

标签: sql query-optimization

SELECT  Q.[Id],[Xml]
    FROM [Tab1] Q 
    FULL OUTER JOIN [Tab2] D 
         ON Q.DispatcherId = D.Id
         Where ([RequestDate] IS NOT NULL)
         and RequestDate < (GETDATE() - cast ((select scd.Value from  dbo.Tab3 as scd where scd .Name like 'SpecificName' ) as int ))
                        Order By [Priority], [ExecuteDate] asc

有没有办法优化where子句,我的目标是从日期比较中的Tab3中选择?

PS:刚刚对select返回的值进行硬编码的区别是00:00:04

1 个答案:

答案 0 :(得分:0)

WHERE-Clause中没有 SUBSELECT:

    DECLARE @myValue int = cast ((SELECT scd.Value 
                                    FROM dbo.Tab3 
                                   WHERE scd .Name = 'SpecificName') as int)
            SELECT Q.[Id]
                  ,[Xml]
              FROM [Tab1] Q 
   FULL OUTER JOIN [Tab2] D 
                ON Q.DispatcherId = D.Id
             WHERE RequestDate < (GETDATE() - @myValue )
          ORDER BY [Priority], [ExecuteDate] ASC