Hi Everyone,
I have a User Defined Function that currently takes about 6 minutes to process 100,000 records. Given that I need to process about 3 million records I am naturally keen to optimise this UDF if at all possible.
The UDF basically creates a string token based on two strings passed as parameters to the function. It is relatively self explanatory, I hope, and I have included the UDF below for your reference.
I would be grateful if anyone could suggest how I could go about optimising this.
Many Thanks,
John
I have a User Defined Function that currently takes about 6 minutes to process 100,000 records. Given that I need to process about 3 million records I am naturally keen to optimise this UDF if at all possible.
The UDF basically creates a string token based on two strings passed as parameters to the function. It is relatively self explanatory, I hope, and I have included the UDF below for your reference.
I would be grateful if anyone could suggest how I could go about optimising this.
Many Thanks,
John
Code:
CREATE FUNCTION cdsCalculateMatchKey1
(@companyColumn varchar(255), @postcodeColumn varchar(100) )
RETURNS varchar(20) AS
BEGIN
--Declare Counters
declare @consonantCounter int
declare @consonantLimit int
declare @vowelCounter int
declare @vowelLimit int
declare @numeralCounter int
declare @numeralLimit int
declare @stringLength int
declare @stringIndex int
--Declare string processing variables
--declare @companyColumn Varchar(255)
declare @companyToken varchar(255)
declare @currentCharacter varchar(1)
--Set initial variable statuses
set @consonantCounter = 0
set @consonantLimit = 5
set @vowelCounter = 0
set @vowelLimit = 3
set @stringIndex = 0
set @numeralCounter = 0
set @numeralLimit = 2
set @companyColumn = upper(replace(@companyColumn,' ',''))
set @postcodeColumn = upper(replace(@postcodeColumn,' ',''))
set @companyToken = ''
set @stringLength = len(@companyColumn)
--Begin querying the @companyColumn string and extrapolating the desired character data
while(@stringIndex <= @stringLength and
(@consonantCounter<@consonantLimit or @vowelCounter<@vowelLimit or @numeralCounter<@NumeralLimit))
begin
select @currentCharacter = substring(@companyColumn,@stringIndex,1)
--Print @currentCharacter
if exists(select @currentCharacter
where @currentCharacter in('b','c','d','f','g','h','j','k','l','m',
'n','p','q','r','s','t','v','w','x','y','z'))
begin
if(@consonantCounter<@consonantLimit)
begin
--print 'Consonant ' + @currentCharacter
set @consonantCounter = @consonantCounter + 1
set @companyToken = @companyToken + @currentCharacter
end
end
else if exists(select @currentCharacter where @currentCharacter in('a','e','i','o','u'))
begin
if(@vowelCounter<@vowelLimit)
begin
--Print 'Vowel ' + @currentCharacter
set @vowelCounter = @vowelCounter + 1
set @companyToken = @companyToken + @currentCharacter
end
end
else if exists(select @currentCharacter where isnumeric(@currentCharacter)=1)
begin
if(@numeralCounter<@numeralLimit)
begin
--Print 'Vowel ' + @currentCharacter
set @numeralCounter = @numeralCounter + 1
set @companyToken = @companyToken + @currentCharacter
end
end
set @stringIndex = @stringIndex + 1
end
--Define the matchkey and return its value
return @postcodeColumn+@companyToken
END