October 8, 2013 at 5:46 am
Hi all i have tried below two functions to split string,but both are working slow:
Function 1 using substring and while loop
ALTER function [dbo].[uf_split](
@String nvarchar (MAX),
@Delimiter nvarchar (10)
)
returns @ValueTable table ([Value] nvarchar(MAX))
begin
declare @NextString nvarchar(MAX)
declare @Pos int
declare @NextPos int
declare @CommaCheck nvarchar(1)
--Initialize
set @NextString = ''
set @CommaCheck = right(@String,1)
--Check for trailing Comma, if not exists, INSERT
--if (@CommaCheck <> @Delimiter )
set @String = @String + @Delimiter
--Get position of first Comma
set @Pos = charindex(@Delimiter,@String)
set @NextPos = 1
--Loop while there is still a comma in the String of levels
while (@pos <> 0)
begin
set @NextString = substring(@String,1,@Pos - 1)
insert into @ValueTable ( [Value]) Values (@NextString)
set @String = substring(@String,@pos +1,len(@String))
set @NextPos = @Pos
set @pos = charindex(@Delimiter,@String)
end
return
end
FUNCTION 2 with XML:
ALTER function [dbo].[split] (
@qustion_option_id varchar(max),
@separator char )
RETURNS @list TABLE (item varchar(max))
BEGIN
DECLARE @xml XML
SELECT @xml = CAST ( '<A>' + REPLACE ( @qustion_option_id, @separator, '</A><A>') + '</A>' AS XML)
INSERT
INTO @list
SELECT t.value('.', 'varchar(max)')
FROM @xml.nodes('/A') AS xml(t)
RETURN
END
any other way to do the split function efficiently?
October 8, 2013 at 5:56 am
Not only a faster solution, but a comparison of many including the two you've listed: DelimitedSplit8k[/url] by Jeff Moden et al.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 8, 2013 at 8:07 am
but even this is also taking more time for 25K records.
with Improved SQL 8K “CSV Splitter” Function its taking around 25 sec
with xml its taking around 30sec
with substring its taking more than one min
October 8, 2013 at 8:09 am
sathiyan00 (10/8/2013)
but even this is also taking more time for 25K records.with Improved SQL 8K “CSV Splitter” Function its taking around 25 sec
with xml its taking around 30sec
with substring its taking more than one min
Post your query.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 8, 2013 at 8:18 am
sathiyan00 (10/8/2013)
but even this is also taking more time for 25K records.with Improved SQL 8K “CSV Splitter” Function its taking around 25 sec
with xml its taking around 30sec
with substring its taking more than one min
On your functions, you're using varchar(max) and it's noted on the article that it should be avoided because it will reduce performance of the 8KDelimitedSplitter.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy