SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need Help for performance


Need Help for performance

Author
Message
patil.yatish
patil.yatish
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 52
Hi All,

I am using a Function code below for each row, this is called for 15000 rows. and it is working very slow.
Can any one please help me


Declare
@InputString VARCHAR(8000),
@Delimiter VARCHAR(50),
@ConsumptionFlags VARCHAR(8000),
@IgnoredConsumptionFlags VARCHAR(8000),
@persistConsumptionFlags VARCHAR(8000),
@persistxml XML


SET @InputString = '0|0|0|1|0|0|54|78|0|0|0|147|0|0|0|0|0|0|0'
SET @Delimiter = '|'
SET @ConsumptionFlags = '0001000000000000000'
SET @IgnoredConsumptionFlags = null
SET @persistConsumptionFlags = '7|7|7|7|7|7|1|1|7|7|7|1|7|7|7|7|1|1'
SET @persistxml = '<Item>7</Item><Item>7</Item><Item>7</Item><Item>7</Item><Item>7</Item><Item>7</Item><Item>1</Item><Item>1</Item><Item>7</Item><Item>7</Item><Item>7</Item><Item>1</Item><Item>7</Item><Item>7</Item><Item>7</Item><Item>7</Item><Item>1</Item><Item>1</Item>'



DECLARE @Result NVARCHAR(Max)

Declare @Items TABLE
(
[Id] [int] IDENTITY(1,1) NOT NULL,
Item VARCHAR(8000)
)


Declare @Persist TABLE
(
[Id] [int] IDENTITY(1,1) NOT NULL,
Item [int]
)



INSERT INTO @Persist
SELECT Item = T.c.value('.','varchar(20)')
FROM @persistxml.nodes('/Item') T(c)
WHERE T.c.value('.','varchar(20)') <> ''



IF @IgnoredConsumptionFlags = ''
SET @IgnoredConsumptionFlags = null


IF @Delimiter = ' '
BEGIN
SET @Delimiter = ','
SET @InputString = REPLACE(@InputString, ' ', @Delimiter)
END

IF (@Delimiter IS NULL OR @Delimiter = '')
SET @Delimiter = ','


DECLARE @Item VARCHAR(8000)
DECLARE @ItemList VARCHAR(8000)
DECLARE @DelimIndex INT
DECLARE @Index INT


SET @ItemList = @InputString
SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
SET @Index = 1

WHILE (@DelimIndex != 0)
BEGIN
SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)

IF (Convert(INT,@Item) = 1 and 0 = (SUBSTRING(@ConsumptionFlags, @Index, 1)))
BEGIN
SET @Item = Convert(INT,@Item) + 1
--PRINT Convert(varchar,@Index) + ' - ' + SUBSTRING(@ConsumptionFlags, @Index, 1) + ' - ' + Convert(varchar, Convert(INT,@Item) + 1)
END
INSERT INTO @Items VALUES (@Item)


SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
SET @Index = @Index+ 1
END

IF @Item IS NOT NULL
BEGIN
SET @Item = @ItemList

IF (Convert(INT,@Item) = 1 and 0 = (SUBSTRING(@ConsumptionFlags, @Index, 1)))
BEGIN
SET @Item = Convert(INT,@Item) + 1
END

INSERT INTO @Items VALUES (@Item)
END
ELSE INSERT INTO @Items VALUES (@InputString)


-- Delete Condition which we do not want to Show / Select -- Block Start
DELETE @Items WHERE ID IN ( 9,10,12,17)

IF ( ( Select Count(*) FROM @Items WHERE ID in (18,19)) = 2)
BEGIN
IF EXISTS(
Select *
FROM @Items I
JOIN @Persist SI ON SI.ID = I.ID
WHERE I.Item <> 0 AND Convert(INT,I.Item) <= Convert(INT,SI.Item)
AND I.ID = 18
)
BEGIN
DELETE @Items WHERE ID = 19
END
END

IF ( ( Select Count(*) FROM @Items WHERE ID = 1 and Item > 0) = 1)
BEGIN
IF EXISTS(
Select *
FROM @Items I
JOIN @Persist SI ON SI.ID = I.ID
WHERE I.Item <> 0 AND Convert(INT,I.Item) <= Convert(INT,SI.Item)
AND I.ID = 1
)
BEGIN
DELETE @Items WHERE ID = 11
END
END

-- Block End
DELETE @Items WHERE ID IN (17,18,19) and Item > 1

SELECT @Result = COALESCE(@Result + '|', '') + Convert(varchar,I.Id) + ',' + Convert(varchar,I.Item)
FROM @Items I
JOIN @Persist SI ON SI.ID = I.ID
WHERE I.Item <> 0 AND Convert(INT,I.Item) <= Convert(INT,SI.Item)
AND I.ID NOT IN
(

select ID
from [SplitFunction_Condition](@IgnoredConsumptionFlags,'') WHERE Item <> 0
)

Select @Result




GilaMonster
GilaMonster
SSC Guru
SSC Guru (903K reputation)SSC Guru (903K reputation)SSC Guru (903K reputation)SSC Guru (903K reputation)SSC Guru (903K reputation)SSC Guru (903K reputation)SSC Guru (903K reputation)SSC Guru (903K reputation)

Group: General Forum Members
Points: 903928 Visits: 48773
Please don't post multiple threads for the same question.
No replies here. Replies to https://www.sqlservercentral.com/Forums/1914800/Need-Help-for-performance

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (391K reputation)SSC Guru (391K reputation)SSC Guru (391K reputation)SSC Guru (391K reputation)SSC Guru (391K reputation)SSC Guru (391K reputation)SSC Guru (391K reputation)SSC Guru (391K reputation)

Group: General Forum Members
Points: 391587 Visits: 42852
Sure, check out this article and discussion: http://www.sqlservercentral.com/articles/Tally+Table/72993/

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search