Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Java's String.hashCode() function in T-SQL Expand / Collapse
Author
Message
Posted Thursday, June 12, 2008 11:40 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, May 15, 2014 6:04 AM
Points: 91, Visits: 108
Comments posted to this topic are about the item Java's String.hashCode() function in T-SQL
Post #516137
Posted Wednesday, January 28, 2009 8:58 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, April 18, 2014 1:58 PM
Points: 438, Visits: 902
Thanks for a very useful function. When applied to a scalar value, the routine is fine. However, if you're applying it to thousands of rows, it's very slow because of the while loop.

Here's an alternative.
alter function dbo.UTIL_JAVA_HASHCODE(@str varchar(max))
returns int
as
-------------------------------------------------------------------------------
-- Proc: UTIL_JAVA_HASHCODE
-- Desc: Replicate Java's String.HashCode() function
-- Inputs: @str: String
-- Outputs: Java hashcode of the string (4 byte integer)
-------------------------------------------------------------------------------
begin
declare @h bigint
set @h = 0
select @h = (@h*31 + ascii(substring(@str,X.pos,1)))%4294967296
from (select top(len(@str))
row_number() over (order by getdate()) as pos
from sys.all_objects) as X
if @h >= 2147483648 set @h = @h - 4294967296
return convert(int, @h)
end;
go

With the following logic:
set statistics time on
go
declare @table table ( hashCode int )
insert into @table
select global.dbo.UTIL_JAVA_HASHCODE( X.varchar32field )
from TableWith33000Rows as X
go

Using the original function, the insert completed in 53s (avg of 3 runs). Applying the original function to a varchar(64) field, the elapsed time grew to 100s.

After revising the function to eliminate the while loop, the varchar(32) processing completes in 11s and the varchar(64) processing in only 14s. (If you have a tally table feel free to use it instead of the derived table from sys.all_objects.)
:)
Post #645007
Posted Wednesday, January 28, 2009 9:05 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, May 15, 2014 6:04 AM
Points: 91, Visits: 108
Thanks for this enhancement! I have always been reluctant to use the "select @var=@var + col from tbl" construct, for fear of it stopping working in some future release. But it seems like it's here to stay. And with that kind of performance improvement it's hard to resist.
Post #645014
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse