今天用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????百度下吧。。。。