当前位置:   article > 正文

where 1=-1 and 1=1 会不会影响查询效率?

and 1=1会导致查询变慢吗


           

        今天用sql profiler跟一个底层生成的SQL 的时候,跟到这样一段代码:


     

  1. WITH TempQuery AS
  2. (
  3. SELECT *, ROW_NUMBER() OVER (ORDER BY CreateTime DESC) AS 'RowNumberForSplit'
  4. FROM (select E.Name as Name, U.RealyName as RealyName,C.[Description] as Descriptions,'求职者' as tsf ,C.Result,C.CreateTime from [Mr].[User_Complaint] UC inner join [Mr].[User] U on UC.UserCode=U.Code inner join [Mr].[Complaint] C on UC.ComplaintCode=C.Code inner join [Mr].[Enterprise] E on UC.EnterpriseCode=E.Code union select E.Name as Name, U.RealyName as RealyName,C.[Description] as Descriptions,'企业' as tsf ,C.Result,C.CreateTime from [Mr].[Enterprise_Complaint] EC inner join [Mr].[Enterprise] E on EC.EnterpriseCode=E.Code inner join [Mr].[Complaint] C on EC.ComplaintCode =C.Code inner join [Mr].[User] U on EC.UserCode=U.Code) CP
  5. WHERE 1 = 1 AND 1=1
  6. )
  7. SELECT *
  8. FROM TempQuery
  9. WHERE RowNumberForSplit BETWEEN 1 AND 10;
  10. SELECT COUNT(1) AS TOTAL_COUNT FROM (select E.Name as Name, U.RealyName as RealyName,C.[Description] as Descriptions,'求职者' as tsf ,C.Result,C.CreateTime from [Mr].[User_Complaint] UC inner join [Mr].[User] U on UC.UserCode=U.Code inner join [Mr].[Complaint] C on UC.ComplaintCode=C.Code inner join [Mr].[Enterprise] E on UC.EnterpriseCode=E.Code union select E.Name as Name, U.RealyName as RealyName,C.[Description] as Descriptions,'企业' as tsf ,C.Result,C.CreateTime from [Mr].[Enterprise_Complaint] EC inner join [Mr].[Enterprise] E on EC.EnterpriseCode=E.Code inner join [Mr].[Complaint] C on EC.ComplaintCode =C.Code inner join [Mr].[User] U on EC.UserCode=U.Code) CP WHERE 1 = 1 AND 1=1

       然后你就看到后面跟着的where 1=1 and 1=1,以前也用过这个东西拼过条件,但是后来有人说这样影响查询性能,再后来又有人说不影响。然后我就迷茫了。。。



      还是自己做个实验测试下吧。


       首先,先看一下没有这个条件的查询:


      

  1. /****** Script for SelectTopNRows command from SSMS ******/
  2. SELECT TOP 100000 [RESOURCE_ID]
  3. ,[CLASS]
  4. ,[SORT_ID]
  5. ,[XML_CONTENT]
  6. ,[SEARCH_CONTENT]
  7. ,[ROW_ID]
  8. FROM [MCS_WORKFLOW].[WF].[GENERIC_FORM_RELATIVE_DATA] WHERE 1=1 AND 1=1

       然后使用执行计划来估计下:

         

       




       然后加入条件:


          

      


在执行计划中可以看到,开销几乎全部在聚集索引表的扫描上,对比上图,发现这两张表数据一致。


       

       

      

     嘿嘿,看来他们的查询效率是一样的。


     but why????百度下吧。。。。







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/273371
推荐阅读
相关标签
  

闽ICP备14008679号