或者<dns value="localhost"/>
6、注意between ... and ...在使用变量和使用拼接字符串中不过的执行计划导致的性能问题:
declare@handleTimedatetime; declare@beginTimedatetime; declare@endTimedatetime; set@handleTime=DateAdd(day, -1, getdate()); set@beginTime=CONVERT(datetime, CONVERT(char(10), @handleTime, 120)); set@endTime=DateAdd(day, 1, @beginTime); declare@sqlnvarchar(1000); set@sql='SELECT * FROM [UserPosts] WITH(NOLOCK) WHERE ([AddTime] between '''+ cast(@beginTimeasnvarchar(100)) +''' AND '''+cast(@endTimeasnvarchar(100)) +''')'; --print @sql declare@stdatetime declare@etdatetime set@st=getdate(); exec(@sql); set@et=getdate(); selectdatediff(millisecond, @st, @et); set@st=getdate(); select*from[UserPosts]WITH(NOLOCK) WHERE ([AddTime]between@beginTimeand@endTime); set@et=getdate(); selectdatediff(millisecond, @st, @et);
拼接字符串: 0ms
使用变量:23106ms
7、SQL拆分字符串
CREATEPROCEDURE[dbo].[ec_System_SplitString] @strsnvarchar(4000), @separatornchar(1)=',' AS BEGIN SET NOCOUNT ON; DECLARE@tbNamestable([Name]nvarchar(256) NOTNULLPRIMARYKEY) DECLARE@Numint; DECLARE@Posint; DECLARE@NextPosint; DECLARE@Namenvarchar(256); SET@Num=0; SET@Pos=1; WHILE(@Pos<=LEN(@strs)) BEGIN SELECT@NextPos=CHARINDEX(@separator, @strs, @Pos) IF (@NextPos=0OR@NextPosISNULL) SELECT@NextPos=LEN(@strs) +1 SELECT@Name=RTRIM(LTRIM(SUBSTRING(@strs, @Pos, @NextPos-@Pos))) SELECT@Pos=@NextPos+1 INSERTINTO@tbNamesVALUES (@Name) SET@Num=@Num+1 END SELECT[Name]FROM@tbNames END