Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Better logic than cursor


Better logic than cursor

Author
Message
Compassionate
Compassionate
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 31
Hi All,

I have two tables Tab1 and Tab2.For each row in Tab1, the table Tab2 contains multiple rows.Important columns in both tables are C0,C1 and C2. So i have to update the C1 and C2 columns in Tab1 with count(C1) and max(C2) for each C0.
assume C0="Temp" and Tab2 has 10 rows for Temp.So Tab1 should be updated with C1 as 10 and C2 asmax(C2) in Tab2.

so i wrote a cursor as below.It takes 1 min for two rows in Tab1.Any better solution ?

declare T1_cursor CURSOR for
select distinct C0 from Tab1

OPEN T1_cursor

FETCH NEXT FROM T1_cursor
INTO @C0

WHILE @@FETCH_STATUS = 0
BEGIN
select @recordCount=count(*)+1,@maxTime=max(maxtime) from Tab2
where C0=@C0

update T1 set RecordCount=@recordCount , Maxtime=@maxTime where C0=@C0

FETCH NEXT FROM T1_cursor
INTO @C0

END -- End of cursor

CLOSE T1_cursor
DEALLOCATE T1_cursor
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24229 Visits: 37978
Actually, yes. Don't use a cursor. Oh, wait, you would like some actual code here wouldn't you?

Okay, we can that once you post the DDL (CREATE TABLE statements) for the tables, some sample data (as INSERT INTO statements) for each of the tables, the expected results based on the sample data provided (usually best provided as another table and series of insert statements to make testing/comparisons easier).

Need help with this, read the first article I have referenced below in my signature block. It will help you with everything you need to do to get the best possible answers quickly.

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)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45173 Visits: 39925
Compassionate (3/2/2013)
Hi All,

I have two tables Tab1 and Tab2.For each row in Tab1, the table Tab2 contains multiple rows.Important columns in both tables are C0,C1 and C2. So i have to update the C1 and C2 columns in Tab1 with count(C1) and max(C2) for each C0.
assume C0="Temp" and Tab2 has 10 rows for Temp.So Tab1 should be updated with C1 as 10 and C2 asmax(C2) in Tab2.

so i wrote a cursor as below.It takes 1 min for two rows in Tab1.Any better solution ?

declare T1_cursor CURSOR for
select distinct C0 from Tab1

OPEN T1_cursor

FETCH NEXT FROM T1_cursor
INTO @C0

WHILE @@FETCH_STATUS = 0
BEGIN
select @recordCount=count(*)+1,@maxTime=max(maxtime) from Tab2
where C0=@C0

update T1 set RecordCount=@recordCount , Maxtime=@maxTime where C0=@C0

FETCH NEXT FROM T1_cursor
INTO @C0

END -- End of cursor

CLOSE T1_cursor
DEALLOCATE T1_cursor


I'm really concerned for your system because even a cursor isn't normally as slow as that. There's something really funky going on if it's only doing 2 rows a minute. Perhaps it's just some really big tables that have no indexes. If that's true, even the following set based code may be slower than desired.

And, no... haven't tested it because you didn't post any readily consumable data to test with. Follow the link Lynn pointed to. You'll get much quicker responses and even tested coded answers if you follow the suggestions at that article.

 UPDATE t1
SET RecordCount = preagg.RecordCount,
MaxTime = preagg.MaxTime
FROM dbo.Tab1 t1
JOIN (
SELECT CO, RecordCount = COUNT(*)+1, MaxTime = MAX(MaxTime)
FROM dbo.Tab2
GROUP BY CO
) preagg
ON preagg.CO = t1.CO
;



--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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