sql - Is it faster to check if length = 0 than to compare it to an empty string? -


I have heard that in some programming languages ​​it checks whether the length of the string is 0 , To see if the content is "?

  Select from users user_id Where LEN (USER_EMAIL) = 0  

: Also T-SQL

is true for sample Versus

  user select user_email = ''  

Edit You have updated your question because I saw it before. In that example, I would say that you should always use the user_id select

  where user_email = ''  

no

  Select user_id from users where lEN (user_email) = 0  

will be allowed to use an index before one As the performance optimization, it will be a little bit subtle customization of the string! From [Guru] choose to see this in #temp

 . [Dbo]. [Spt_values] Create the cluster index ix ([name], [number]) at #temp [number]] Select [number] #temp Where [name] = '' [number] #temp from ou LEN ([name]) 

Original Answer

Execution Plans

In (SQL Server 2008 - I "borrow" the structure of time) I found a slight edge to test the length of the content instead, when there is a string in @stringToTest Were when there were zero. I probably did not do enough testing to get the conclusion of any firm.

In a normal execution plan, I imagine that the difference will be very negligible, and if you're comparing that string in TSQL, then any important difference you might have is to use a different language for it. The possibility of doing

  announcement @date datetime2 @testContents announcement announcement iNT @testLength iNT set @testContents = 0 set @testLength = 0 declared @count INT, @value INT, @stringToTest varchar (100) @stringToTest = Set 'jasdsdjkfhjskdhdfkjshdfkjsdehdjfk' set @count = 1 while @count & lt; 10000000 BEGIN SET @date = GETDATE () SELECT @value = Case when @stringToTest = '' then 1 and 0set @testContents = @testContents + DATEDIFF (microsoft, @date, gETDATE ()) SET @date = GETDATE () selection @value = Case when lane (@stringToTest) = 0 set 1 and 0 end then @testLength = @testLength + DateDiff (microsecond, @date, GETDATE ()) Set @count = @count +1 End Select @testContents / 1000000 AS seconds tending content, @ test length / 1000000. AS seconds testing length  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -