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 12»»

Split strings alternative to XML Expand / Collapse
Author
Message
Posted Wednesday, March 27, 2013 2:36 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 9:04 AM
Points: 8, Visits: 71
Hi guys,
For some performance considerations, we would like to try alternative to the code below.
Anyone could help on how to get this job done using CTE, temp table or I don not know...
Actually, I am not a dev guy at all :
Thanks,
Mack

CREATE FUNCTION [dbo].[fn_Split](@data NVARCHAR(MAX), @delimiter NVARCHAR(5))
RETURNS @t TABLE (Idx int identity (1,1), data NVARCHAR(max))
AS
BEGIN

DECLARE @textXML XML;
SELECT @textXML = CAST('<d>' + REPLACE(@data, @delimiter, '</d><d>') + '</d>' AS XML);

INSERT INTO @t(data)
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM @textXML.nodes('/d') T(split)

RETURN



Post #1436162
Posted Wednesday, March 27, 2013 2:44 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 20,806, Visits: 32,738
Check out the resources with this article: http://www.sqlservercentral.com/articles/Tally+Table/72993/. You should find a CLR splitter that will meet your needs.

Be sure to read the article and the discussion with it.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1436167
Posted Wednesday, March 27, 2013 3:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 9:04 AM
Points: 8, Visits: 71
Thanks Lynn,
...I have gone through the article, too much complicated to me and definitely not my stuff...
The thing is I know the function above is killing perf, but I do not know how to write something else despite my readings.
The goal is to submit some results to the dev team and show how crappy then can produce their coding when not being concerned by global performance...
If anyone can "translate" this little function, ideally not in CLR, that would be helpful :)



Post #1436175
Posted Wednesday, March 27, 2013 3:02 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 20,806, Visits: 32,738
Does your function have to split a string declared as nvarchar(max)?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1436176
Posted Wednesday, March 27, 2013 3:17 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 9:04 AM
Points: 8, Visits: 71
Not sure this is absolutely necessary, maybe varchar(some values) could fit, but as of now, I do not know "where" they could use this function to split "what" amout of data...
It could some comments in a text box as it is commercial app...
nvarchar(max) type should be tested at least to compare performance.



Post #1436185
Posted Wednesday, March 27, 2013 3:28 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 20,806, Visits: 32,738
The two T-SQL functions, DelimitedSplit8K and DelimitedSplitN4K, that are attached to the article I referred you to are optimized to split varchar(8000) and nvarchar(4000) strings.

I would read the article and discussion again, as use these functions on appropriate sized strings to see how they work.

For string longer than varchar(8000) and nvarchar(4000) you will probably want to go to a CLR splitter as it will out perform a T-SQL based splitter.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1436190
Posted Wednesday, March 27, 2013 4:24 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 9:04 AM
Points: 8, Visits: 71
I am trying...but I do not succeed in adapting the 8k function.
Don't know how to "integrate" the <d>, <d/>, etc stuff as they exist in the initial function...
I really suck



Post #1436213
Posted Wednesday, March 27, 2013 5:57 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 9:04 AM
Points: 8, Visits: 71
I have got it...

create function Split_fnOK
(
@data varchar(8000),
@deli_char varchar(3)
)
returns @list table
(
Idx int,
data varchar(8000)
)
as
begin

declare @from_loc int
declare @to_loc int

if charindex(@deli_char,@data,0) <= 0
begin
insert into @list(Idx, data) values (1, @data)
return
end

if charindex(@deli_char,@data,0) > 0
begin
select @from_loc = 0
select @to_loc = charindex(@deli_char,@data,0)
end

if charindex(@deli_char,@data,0) <= 0
begin
select @to_loc = null
end

while @to_loc is not null
begin

if substring(@data,@from_loc, @to_loc - @from_loc) <> ''
begin
insert into @list(Idx, data)
select isnull(max(Idx),0) + 1, substring(@data,@from_loc, @to_loc - @from_loc)
from @list
end

select @from_loc = charindex(@deli_char,@data,@from_loc+len(@deli_char)) + len(@deli_char)
select @to_loc = charindex(@deli_char,@data,@from_loc)


if @to_loc = 0
begin
if substring(@data,@from_loc, (len(@data) - @from_loc) + len(@deli_char)) <> ''
begin
insert into @list(Idx, data)
select isnull(max(Idx),0) + 1, substring(@data,@from_loc, (len(@data) - @from_loc) + len(@deli_char))
from @list
end
select @to_loc = null
end
end
return
end
go

With that set up, I have got much better stats time/cpu and I/O. Good.
Cheers



Post #1436231
Posted Thursday, March 28, 2013 8:03 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 13,230, Visits: 12,709
MackF (3/27/2013)
I have got it...

...

With that set up, I have got much better stats time/cpu and I/O. Good.
Cheers


This is going to be a LOT slower than the methods Lynn suggested. Actually orders of magnitude slower. I know you have been pointed to it before but check out the article in my signature about splitting strings. Then compare your looping function to DelimitedSplit8K and see which one is faster.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436480
Posted Thursday, March 28, 2013 8:05 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:12 AM
Points: 35,589, Visits: 32,179
MackF (3/27/2013)
I have got it...

create function Split_fnOK
(
@data varchar(8000),
@deli_char varchar(3)
)
returns @list table
(
Idx int,
data varchar(8000)
)
as
begin

declare @from_loc int
declare @to_loc int

if charindex(@deli_char,@data,0) <= 0
begin
insert into @list(Idx, data) values (1, @data)
return
end

if charindex(@deli_char,@data,0) > 0
begin
select @from_loc = 0
select @to_loc = charindex(@deli_char,@data,0)
end

if charindex(@deli_char,@data,0) <= 0
begin
select @to_loc = null
end

while @to_loc is not null
begin

if substring(@data,@from_loc, @to_loc - @from_loc) <> ''
begin
insert into @list(Idx, data)
select isnull(max(Idx),0) + 1, substring(@data,@from_loc, @to_loc - @from_loc)
from @list
end

select @from_loc = charindex(@deli_char,@data,@from_loc+len(@deli_char)) + len(@deli_char)
select @to_loc = charindex(@deli_char,@data,@from_loc)


if @to_loc = 0
begin
if substring(@data,@from_loc, (len(@data) - @from_loc) + len(@deli_char)) <> ''
begin
insert into @list(Idx, data)
select isnull(max(Idx),0) + 1, substring(@data,@from_loc, (len(@data) - @from_loc) + len(@deli_char))
from @list
end
select @to_loc = null
end
end
return
end
go

With that set up, I have got much better stats time/cpu and I/O. Good.
Cheers


Show us 2 lines of sample data to split so we can show you how to blow the doors off of everything but a CLR and come pretty close to that, to boot.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1436808
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse