码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-08 13:16:54      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:sql   性能   测试   数据   


           

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


     

WITH TempQuery AS
(
    SELECT *, ROW_NUMBER() OVER (ORDER BY  CreateTime DESC) AS 'RowNumberForSplit'
	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
	
)
SELECT * 
FROM TempQuery 
WHERE RowNumberForSplit BETWEEN 1 AND 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,以前也用过这个东西拼过条件,但是后来有人说这样影响查询性能,再后来又有人说不影响。然后我就迷茫了。。。



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


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


      

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

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

         

        技术分享




       然后加入条件:


           技术分享

      


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


        技术分享

       

      

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


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







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

标签:sql   性能   测试   数据   

原文地址:http://blog.csdn.net/lhc1105/article/details/44937515

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!